循环不继续无法弄清楚为什么

时间:2018-09-21 17:46:30

标签: c++ iostream

嘿,我在这里有这个程序

#include <iostream>

using namespace std;

int main()
{
  int MyNum;
  int ComNum;
  MyNum = 5;

   do {
     cout << "Enter a whole number between 1 and 10: ";
     cin >> ComNum;
     if (ComNum > MyNum) {
       cout << "Sorry that is incorrect (Hint: too high)";
     }
     if (ComNum < MyNum) {
       cout << "Sorry that is incorrect (Hint: too low)";
       cin >> ComNum;
     }
   } while(MyNum != ComNum);
   cout << "Correct"
}

而且我不知道为什么在第一个不正确的答案后它将无法继续运行,我确定我错过了一些小而愚蠢的东西(PS),这是我猜数字游戏的猜测,如果您不能从代码

2 个答案:

答案 0 :(得分:1)

缺少分号后,将给出错误提示:

#include <iostream>

using namespace std;

int main()
{
  int MyNum;
  int ComNum;
  MyNum = 5;

   do {
     cout << "Enter a whole number between 1 and 10: ";
     cin >> ComNum;
     if (ComNum > MyNum) {
       cout << "Sorry that is incorrect (Hint: too high)";
     }
     if (ComNum < MyNum) {
       cout << "Sorry that is incorrect (Hint: too low)";
       cin >> ComNum;
     }
   } while(MyNum != ComNum);
   cout << "Correct";
}
正如其他人所说,第二个cin >> ComNum; 尽管它可以在https://www.onlinegdb.com/online_c++_compiler上使用

,但这是不必要的

答案 1 :(得分:0)

只需在IF中删除cin >> ComNum;,然后修复编译器错误cout << "Correct";