c++ - 对我是否需要一个标记值来终止我的 do while 循环以允许多个条目感到困惑

标签 c++ visual-studio-2010

我要使用一个 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/

相关文章:

C++ win32 更改轨迹栏背景颜色

c# - Visual Studio,如何更改类的颜色?

c++ - 错误映射 PBO cudaGraphicsResource

C++ 无效操作数错误

c++ - 如何使 vector 分配新的(而不是更多的)内存

c++ - 编译器不允许我使用 "DDSTextureLoader.h"和 "WICTextureLoader.h"

visual-studio-2010 - 为 Visual Studio 应用程序设置平台目标的目的是什么?

c++ - 基于模板类型重构c++模板类

c# - REngine' 不包含 'SetDllDirectory' 的定义,'RDotNet

c# - 为什么我在文本框中得到无效输出?