简体   繁体   English

我哪里做错了? 程序未读取“未完成”和“不满意”变量

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

When I run it's not reading outstanding and unsatisfactory .当我运行时,它的阅读并不outstanding且不unsatisfactory The grade and everything seem to work fine.成绩和一切似乎都很好。

a.一种。 Write a program to read in a collection of exam scores ranging in value from 0 to 100. Your program should display the category of each score.编写一个程序来读取一组从 0 到 100 的考试分数。您的程序应该显示每个分数的类别。 It should also count and display the number of outstanding scores (90 to 100), the number of satisfactory scores (60 to 89), and the number of unsatisfactory scores(0 to 59).还应统计并显示优秀分数(90~100)、合格分数(60~89)、不合格分数(0~59)。

b.Modify your program so it also displays the average score at the end of the run.修改您的程序,使其在运行结束时也显示平均分数。

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++;
}

Can you see whats going wrong ?你能看出哪里出了问题吗? Rethink the logic for a score of 75. It will do satis++.重新思考 75 分的逻辑。它会做 satis++。 Fine.美好的。 But what about 45 ?但是 45 呢? It will never pass the else if in the first place, so it will never reach unsatis++ (it doesnt even reach the inner if-statement).它永远不会通过 else if 首先,所以它永远不会到达 unsatis++ (它甚至不会到达内部的 if 语句)。

What you want is more like你想要的更像

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

or even shorter (if scores can be safely assumed to be between 0-100 all the time. :甚至更短(如果可以安全地假设分数始终在 0-100 之间。:

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

I will leave the outstanding part up to you, since this is obviously a homework.我将把优秀的部分留给你,因为这显然是一个家庭作业。 We are glad to help you understand things, but we won't do your schoolwork so you would never learn it.我们很乐意帮助您理解事物,但我们不会做您的功课,因此您永远不会学到它。 A good way of finding bugs is "stepping" through it with a debugger.查找错误的一个好方法是使用调试器“逐步”完成它。 You can tell your IDE to stop at the moment of entering the while-loop and then go though it line by line.您可以告诉您的 IDE 在进入 while 循环的那一刻停止,然后逐行浏览它。 On every line-halt you can see which line its at (and which got skipped..) and what the variable contents are at that particular point in time.在每一行暂停时,您都可以看到它在哪一行(以及哪一行被跳过了......)以及在那个特定时间点的变量内容是什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM