2017-03-15 3 views
-2

私がしていることについては、負ではない値を受け入れることができ、100より大きくすることはできません。残りのプログラムは、 。私は絶対値を使用するなどのいくつかのことを試しましたが、まだうまく機能しませんでした。どんな助けでも大歓迎です。C++で100以下の正の値のみを受け入れる

#include <iostream> 
 
#include <cmath> 
 

 
using namespace std; 
 

 
void getScore(double[]); 
 
double calcAverage(double[]); // Make it a double because you want the exact answer not a rounded one 
 
double findLowest(double[]); 
 

 

 
void main() { 
 

 
\t double scores[5]; // We want to store 5 answers 
 
\t double averageScore, lowestScore; 
 
\t getScore(scores); // Collects the Input 
 
\t averageScore = calcAverage(scores); // The average 
 
\t lowestScore = findLowest(scores); // Finds the lowest score that was put into the array 
 

 
\t cout << "Here are the Results!" << endl; 
 
\t cout << "The average score was a: " << averageScore << endl; 
 
\t cout << "The lowest score was a : " << lowestScore; 
 
\t cout << endl; 
 

 

 
\t cin.ignore(); 
 
\t cin.get(); 
 

 
} 
 

 
void getScore(double scores[]) 
 
{ 
 
\t for (int i = 0; i < 5; i++) // Will loop 5 times for 5 answers 
 
\t { 
 
\t \t cout << "Enter The Test Score Here?: " << endl; 
 
\t \t cin >> scores[i];// will store the inputs that you enter 
 

 
\t \t { 
 
\t \t \t if (i > 100) 
 
\t \t \t { 
 
\t \t \t \t cout << "Not a valid answer try again"; 
 
\t \t \t \t cin >> scores[1]; 
 
\t \t \t } 
 
\t \t } 
 
\t } 
 
} 
 

 
double calcAverage(double scores[]) 
 
{ 
 
\t double temp = 0.00; // It's 0.00 because we need decimals 
 

 
\t for (int i = 0; i < 5; i++) 
 
\t { 
 
\t \t temp += scores[i]; // adding the score values to temp 
 
\t } 
 
\t temp /= 5; // Divides all of the scores by 5 to find the average 
 
\t return temp; 
 
} 
 

 

 
double findLowest(double scores[]) 
 
{ 
 
\t double temp = 0.00; 
 

 
\t for (int i = 0; i < 5; i++) 
 
\t { 
 
\t \t temp = scores[i]; 
 
\t \t if (scores[i - 1] > scores[i]) { 
 
\t \t \t scores[i] = scores[i - 1]; 
 
\t \t \t scores[i - 1] = temp; 
 
\t \t } 
 
\t } 
 
\t temp = scores[0]; // Array starts at the lowest value 
 
\t return temp; 
 
}

+0

)(I> 100)'それは(スコア[I]> 100)を ' – JackVanier

答えて

-1

JackVanier iは[i]は100の代わりに、得点、その後も大きくなるようにするために、あなたがチェックし、述べたように、これは、まず第一に間違っ

if (i > 100) 
{ 
    cout << "Not a valid answer try again"; 
    cin >> scores[1]; 
} 

です。さらにもう1つ:ユーザーが間違った値を再度入力すると、それを受け入れ、常に配列の1位になります。有効な値まで再入力するようにお願いします。

利用代わりに、この: `getScore(中

while (scores[i] < 0.0 || scores[i] > 100.0) 
{ 
    cout << "Not a valid value! Enter again: "; 
    cin >> scores[i]; 
} 
+0

良いアイデアがで配置する場合は、'するかどう 'あなたは'持っています'cin'がまだ有効であることをテストし、もしそうでなければ処理し、無視して、絶望的に悪い入力に対する無限ループを防ぐ。 – user4581301

関連する問題