为什么这个程序不循环?
Why this program doesn't go in loop?
当我给出错误的输入(如字符串"abx")时,它会给我错误消息,但不会返回循环或要求我重试。此示例来自谷歌 c++ 课程示例 4。
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main()
{
int isecret, iguess, chance;
srand(time(NULL));
isecret = rand() %100 + 1;
cout<<"Enter integers between 1 to 100"<<endl;
//cout<<isecret<<endl;
chance = 0;
do{
if (!(cin>>iguess))
{
cout<<"please enter integers between 1 to 100"<<endl;
cin.clear();
cin.ignore(10000/'n');
}
else
{
if (isecret>iguess)
{
cout<<"Your guess is less than the secret"<<endl;
}
else if (isecret<iguess)
{
cout<<"Your guess is more than the secret"<<endl;
};
chance = chance + 1;
}
}
while(iguess!=isecret);
cout<<"Congratulation you guess the correct number: "<<isecret<<" in "<<chance<<" chances"<<endl;
return 0;
}
你错过了一个逗号。所以,替换
cin.ignore(10000/'n');
跟
cin.ignore(10000,'n');
参见 cin.ignore() 的参考资料
相关文章:
- C++程序在循环后给出奇怪的int值
- 我试图制作一个程序,要求用户输入问题和答案,但程序循环不正确
- 为什么我的程序在for循环中k=0时返回垃圾值
- 密码登录程序将永远循环并显示不正确的结果
- 程序在尝试猜测它选择的随机数时进入无限循环?
- 程序以使用 C++ 中的 while 循环查找一组数字的最小值
- 而循环:简单的除法程序输出零,不明白为什么
- 在这个无限循环中,当输入 0 时终止,当输入 int 范围之外的任何内容时,程序行为不正常
- 虽然循环在使用回车键时不退出程序
- 如何调试我的 while 循环C++程序?
- if/else 循环:C++程序:不会显示最终提示/最终循环
- 简单的C 嵌套以用于循环程序,以打印带有星星错误的正方形
- 带有空间的C 星号循环程序
- 循环程序的行为是不可预测的
- 如何解决嵌套循环程序的问题
- 对我的做边循环程序感到困惑
- gcc和clang都无法编译循环程序
- 需要帮助do-while循环程序?(c++)
- 一个用c++编写的简单游戏循环程序
- 如何循环程序,使其继续执行