-9

0 未満または 18 を超える値を入力した場合、10 歳の学生の年齢をユーザーに尋ねようとしています。その値を 0 に変更します。

私は何を間違えましたか?

#include <iostream>
using namespace std;
int main()
   int age [10];
   int TotalAge, AverageAge;

   for (int i = 0; i < 10; i++) 
   {
     cout << "please enter the age of students:";
     cin >> age[i]; 

     if age[i] < 0 || age[i] > 
     cout << "An error was detected in your input, invalid age"; // print 
     age[i] = 0;
     TotalAge += age[i]; // total age is the sum of age
     AverageAge = TotalAge / 10; 
     cout << "Average age of class is: " << AverageAge << endl
   }
4

2 に答える 2

5

if ( age[i] < 0 || age[i] > 18 )

平均をループの外に置く必要があります。

//Initialize variables
int TotalAge = 0, AverageAge = 0;
for (int i = 0; i < 10; i++) 
   {
   cout << "please enter the age of students:";
   cin >> age[i]; 

   if ( age[i] < 0 || age[i] > 18 )
   {
       cout << "An error was detected in your input, invalid age"; // print 
       age[i] = 0;
   }
   TotalAge += age[i]; // total age is the sum of age
}
//Calculate average outside
AverageAge = TotalAge / 10; 
cout << "Average age of class is: " << AverageAge << endl;
于 2012-06-02T09:17:59.517 に答える
3

メイン関数内のすべてが { } の間にある必要があります

于 2012-06-02T09:20:04.193 に答える