循环最多运行一次(循环增量从未执行)

3

我在XCode 6.3.2中收到以下错误:

循环最多只会运行一次(循环增量从未执行)

我尝试使用for (int prob = 0; prob < response; prob++),但仍然收到相同的错误。

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
    unsigned seed;
    int randa, randb, answer, correct;

    seed = static_cast<unsigned int>(time(0));
    srand(seed);

    while (true)
    {
        int response = ' ';
        cout << "How many equations would you like to do? \n";
        cin >> response;

        for (int prob = response; prob > 0; prob--)
        {
            cout << "Calculate the following equation: \n";
            correct = rand() % 100 + 0;
            randa = correct - rand() % correct + 0;
            randb = correct - randa;
            cout << randa << " + " << randb << " = ";
            cin  >> answer;
            if (answer == correct)
            {
                cout << "Correct!\n";

            }
            else
            {
                cout << "Incorrect. The correct answer is: " << correct << "\n";
            }


            return 0;
        }

    }

}
2个回答

9
我尝试了两个不同的编译器(Windows/MSVC 和 Linux/g++)......我没有收到那个警告。
然而 - 你正在从循环中"返回0"。
这当然违背了循环的目的 :)
也导致程序终止。
将"返回"移到循环外,生活就会美好:)

非常感谢!搞定了! - Mark

4

将此行移动:

return 0;

它将结束循环(包括函数和程序!)。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接