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

匿名 (未验证) 提交于 2019-12-03 01:12:01

问题:

I am receiving this error in XCode 6.3.2:

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

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

回答1:

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

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 :)



回答2:

Move this line:

return 0; 

It will end the loop (function as well as program!).



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!