循环最多运行一次(循环增量从未执行) [英] Loop will run at most once (loop increment never executed)

查看:931
本文介绍了循环最多运行一次(循环增量从未执行)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在XCode 6.3.2中收到此错误:

I am receiving this error in XCode 6.3.2:

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

Loop will run at most once (loop increment never executed)

我尝试了for (int prob = 0; prob < response; prob++)并收到了相同的错误.

I have tried for (int prob = 0; prob < response; prob++) and received the same error.

#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;
        }

    }

}

推荐答案

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

I tried two different compilers (Windows/MSVC and Linux/g++) ... and I don't get that warning.

但是-您正在从循环中执行返回0".

However - you're doing a "return 0" from your loop.

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

Which, of course, defeats the purpose of having a loop :)

并且恰好终止了您的程序.

And also happens to terminate your program.

将返回"移到循环外,生活应该是美好的:)

Move the "return" outside of the loop, and Life should be Good :)

这篇关于循环最多运行一次(循环增量从未执行)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆