我哪里做错了?程序未读取"outstanding"和"unsatisfactory"变量
Where did I go wrong? Program is not reading "outstanding" and "unsatisfactory" variables
当我运行时,它不是在读取outstanding
和unsatisfactory
。成绩和一切似乎都很好。
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循环时停止,然后逐行执行。在每一次行停顿中,你都可以看到哪一行在(哪一行被跳过了..),以及在那个特定的时间点上变量的内容是什么。
相关文章:
- 理解boost::asio-async_read在无需读取内容时的行为
- 使用新行和不使用新行读取文件
- 读取文件并输入到矢量中
- 用c++从输入文件中读取另一行
- 读取文件的最后一行并输入到链接列表时出错
- 在进程中对同一管道进行读取和写入时C++管道出现问题
- 无法找到/读取配置文件.conf-FileIOException
- 如何使用Luacneneneba API正确读取字符串和表参数
- C++将文本文件中的数据读取到结构数组中
- 正在将csv文件读取为双精度矢量
- 为什么 sscanf 无法从一个字符串中读取uint64_t和字符?
- 为什么在读取文件大小时文件IO速度会发生变化
- 正在读取二进制文件(is_open)
- 如何在c++中从文本文件中逐行读取整数
- SSH通过/sbin/SSH无法读取RSA密钥文件(从控制台运行)
- 独立读取-修改-写入顺序
- 从文本文件中读取时钟时间和事件时间并进行处理
- 如何从文本文件中读取值和数组
- 为什么文件名被设置为一个点,而不是在读取矢量中的文件名时
- 我哪里做错了?程序未读取"outstanding"和"unsatisfactory"变量