输入有效的输入值后仍有提示
Still being prompted after entering a valid input value
我这样做是为了分配,但即使我输入了所需范围内的有效输入,我仍然会收到提示。这两个输入提示都会发生这种情况。我怀疑问题出在setupGame
函数的while
块中。
#include <iostream>
using namespace std;
bool setupGame(int numberRef, int triesRef);
int main(){
cout<<"hello world";
setupGame(4,4);
cout<<"enough";
}
//SETUP GAME FUNCTION
bool setupGame(int numberRef, int triesRef) {
do {
cout << "ENTER A NUMBER BETWEEN 1 and 100" << endl;
cin >> numberRef;
cin.clear();
//your code here
cout << "You entered: " << numberRef << endl;
if (numberRef==-1) {
cout << "You do not want to set a number " << endl;
cout << "so you stopped the program" << endl;
}
else if(numberRef >=1 && numberRef <=100)
do {
cout << "ENTER TRIES BETWEEN 3 and 7" << endl;
cin >> triesRef;
cin.clear();
//cin.ignore( 'n');
cout<< "You entered: "<< triesRef<< endl;
if (triesRef==-1) {
cout << "You do not want to set tries. ";
cout << "so you stopped the program" << endl;
} else if(triesRef <= 3 && triesRef >= 7){
cout<<"Number of tries should be between 3 and 7";
}
}
while(numberRef >=1 && numberRef <=100);{
return true;
}
}
while(triesRef >= 3 && triesRef <= 7);{
return true;
} }
您正与这些嵌套循环纠缠在一起。您的提示不断打印,因为while循环:
while(numberRef >=1 && numberRef <=100)
将继续重复它的前一个do
块,直到您输入一个小于1或大于100的值,最后的while
循环也是如此。
我假设您使用cin.clear()
来刷新以前的输入,如果您停止它,那就不是cin.clear()
的目的。相反,它用于清除cin
的错误状态。请在这里仔细阅读。
下面是实现您想要的代码,请观察我是如何在每次cin
提示后实现while循环以确保输入有效字符的。
#include <iostream>
#include <fstream>
using namespace std;
bool setupGame(int numberRef, int triesRef);
int main(){
cout<<"hello world";
setupGame(4,4);
cout<<"enough";
}
//SETUP GAME FUNCTION
bool setupGame(int numberRef, int triesRef) {
cout << "ENTER A NUMBER BETWEEN 1 and 100" << endl;
cin >> numberRef;
while((numberRef < 1 || numberRef > 100 || cin.fail()) && numberRef != -1) {
cin.clear(); // Used to clear error state of cin
cin.ignore(); // Might want to ignore the whole line
cout<<"Number should be between 1 and 100, or -1 , please try again: ";
cin>>numberRef;
}
cout << "You entered: " << numberRef << endl;
if (numberRef==-1) {
cout << "You do not want to set a number " << endl;
cout << "so you stopped the program" << endl;
}
if(numberRef >=1 && numberRef <=100) {
cout << "ENTER TRIES BETWEEN 3 and 7" << endl;
cin >> triesRef;
while(triesRef < 3 || triesRef > 7 || cin.fail()) {
cin.clear(); // Used to clear error state of cin
cin.ignore(); // Might want to ignore the whole line
cout<<"Tries should be between 3 and 7 , please try again: ";
cin>>triesRef;
}
cout<< "You entered: "<< triesRef<< endl;
if (triesRef==-1) {
cout << "You do not want to set tries. ";
cout << "so you stopped the program" << endl;
return false;
}
}
}
相关文章:
- 如何使用递归循环我的代码(当用户输入无效输入时,它会再次提示他们)?
- 如何确保用户在一行上提示输入一次时输入名字和姓氏?
- 为什么 std::cin 不提示用户输入?
- 如何使用打印到 PDF 打印机在 MFC 和 CView 中以编程方式打印而不提示输入文件名?
- 编写一个程序,提示用户输入一个整数,然后输出数字的单个数字和数字的总和
- C++:跳过第一个用户输入提示
- (显示金字塔)编写一个程序,该程序提示用户输入1到15的整数并显示金字塔
- 循环不提示用户输入(C )
- C++ 编写一个程序,提示用户以数字形式输入一个人的出生日期(投掷/接球)
- 使用提示用户输入笛卡尔平面中某些点的x-y坐标(浮点类型)的函数
- 如何提示用户在C++中输入正确的电子邮件地址格式
- 输入有效的输入值后仍有提示
- 提示用户输入,直到C++正确为止
- 将变量添加到作为提示的输入参数
- 提示再次运行程序:修复他们是否输入了无效语句
- Sudoku输入程序跳过提示
- 当提示输入整数值时输入字符时,为什么程序会中断
- qt检查文件中是否存在文件,如果它没有提示用户输入其位置,则将文件复制到程序工作目录
- 提示用户输入两个 3x3 int 数组并检查等效性
- 在 Eclipse 中运行时C++程序的输入提示不出现