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

时间:2015-06-10 05:16:32

标签: c++ for-loop

我在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 个答案:

答案 0 :(得分:6)

我尝试了两种不同的编译器(Windows / MSVC和Linux / g ++)......我没有收到警告。

然而 - 你正在做一个&#34;返回0&#34;来自你的循环。

当然,这违背了循环的目的:)

也恰好终止你的程序。

移动&#34;返回&#34;在循环之外,生活应该是好的:)

答案 1 :(得分:2)

移动此行:

return 0;

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