我哪里做错了?程序未读取"outstanding"和"unsatisfactory"变量

Where did I go wrong? Program is not reading "outstanding" and "unsatisfactory" variables

本文关键字:读取 outstanding 变量 unsatisfactory 程序 错了      更新时间:2023-10-16

当我运行时,它不是在读取outstandingunsatisfactory。成绩和一切似乎都很好。

a。编写一个程序,读取0到100之间的考试成绩。你的程序应该显示每个分数的类别。它还应统计和显示优秀分数(90至100)、满意分数(60至89)和不满意分数(0至59)。

b。修改你的程序,使其在跑步结束时也显示平均分数。

using namespace std;
void displayGrade(int);

int main()
{
   const int SENTINEL = -1;    
   int score, sum = 0, count = 0, outstanding = 0, satisfactory = 0, unsatisfactory  =0;                                
   double average;              
   cout << "Enter scores one at a time as requested." << endl;
   cout << "When done, enter " << SENTINEL << " to finish entering scores." << endl;
   cout << "Enter the first score: ";
   cin >> score;
   while (score != SENTINEL)
   {
      sum += score;
      count++;
      displayGrade(score); 
      cout << endl<< "Enter the next score: ";
      cin >> score;
      if (score >= 90)
          outstanding++;
      else if (score >=60){
          satisfactory++;
          if (score >= 0 && score <= 59)
              unsatisfactory++;
      }
   } 
   cout << endl << endl; 
   cout << "Number of scores processed is " << count << endl;
   cout << "Sum of exam scores is " << sum << endl;
   cout << "The number of Outstanding scores is: " << outstanding << endl;
   cout << "The number of Satisfactory scores is: " << satisfactory << endl;
   cout << "The number of Unsatisfactory scores is: " << unsatisfactory << endl;
   if (count > 0)
   {
      average = sum / count;
      cout << "Average score is " << average << endl;
   }
   system("PAUSE");
   return 0;
}   
void displayGrade(int score)
{
   if (score >= 90)
       cout << "Grade is A" << endl;
   else if (score >= 80)
       cout << "Grade is B" << endl;
   else if (score >= 70)
       cout << "Grade is C" << endl;
   else if (score >= 60)
       cout << "Grade is`enter code here` D" << endl;
   else
       cout << "Grade is F" << endl;
}
else if (score >=60){
  satisfactory++;
  if(score >= 0 && score <= 59)
    unsatisfactory++;
}

你能看到出了什么问题吗?重新思考75分的逻辑。它会做satis++。好的但45呢?它永远不会超过else-if,所以它永远不会到达unsatis++(它甚至不会到达内部if语句)。

你想要的更像

else if (score >=60){
  satisfactory++;
} else if(score >= 0 && score <= 59) {
  unsatisfactory++;
}

或者更短(如果可以安全地假设分数一直在0-100之间。:

else if (score >=60){
  satisfactory++;
} else {
  unsatisfactory++;
}

我将把突出的部分留给你,因为这显然是一个家庭作业。我们很乐意帮助您理解一些东西,但我们不会做您的作业,所以您永远不会学习。发现错误的一个好方法是使用调试器"逐步"完成它。您可以告诉IDE在进入while循环时停止,然后逐行执行。在每一次行停顿中,你都可以看到哪一行在(哪一行被跳过了..),以及在那个特定的时间点上变量的内容是什么。