我要使用一个 do while 循环来打印平均成绩和字母成绩,但随后我被要求使用另一个循环来确定多个成绩。
我的问题是我的 do while 循环进入无限循环,我不确定何时需要终止它。
#include <iostream>
using namespace std;
int main()
{
float avgscore;
int count;
cout << "Please enter enter your average score inpercent format. I.E. 95.6%" << endl;
cin >> avgscore;
do
{
if (avgscore >= 90)
cout << "Your exam score is: " << avgscore << " Which results in the grade an A!"<<endl;
else if (avgscore >= 80)
cout << "Your exam score is: " << avgscore << " Which results in the grade of a B!" << endl;
else if (avgscore >= 70)
cout << "Your exam score is: " << avgscore << " Which results in the grade of a C!" << endl;
else if (avgscore >= 60)
cout << "Your exam score is: " << avgscore << " Which results in the grade of a D!" << endl;
else if (avgscore <= 59.9)
cout << "Your exam score is: " << avgscore << " Which results in the grade of a F!" << endl;
}
while (avgscore > 0); //This is my problem here, but i'm not sure what i need here to end it.
// I need to add another loop here to allow for more than one entry.
return 0;
}
如有任何帮助,我们将不胜感激!
最佳答案
你可以简单地把下面几行
cout << "Please enter enter your average score inpercent format. I.E. 95.6%" << endl;
cin >> avgscore;
在do-while
里面能够通过输入一个值为 <=0
的值来终止循环.
这里的关键思想是您需要能够更改avgscore
循环内的值 这样检查条件就会因为false
如果你愿意的话。
关于c++ - 对我是否需要一个标记值来终止我的 do while 循环以允许多个条目感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22206309/