C++ 输入有效的输入值后仍会出现提示

C++ 输入有效的输入值后仍会出现提示,c++,C++,我这样做是为了分配任务,但即使我输入了所需范围内的有效输入,仍然会收到提示。两个输入提示都会出现这种情况。我怀疑问题出在setupGame函数的while块中 #include <iostream> using namespace std; bool setupGame(int numberRef, int triesRef); int main(){ cout<<"hello world"; setupGame(4,4); cout<&l

我这样做是为了分配任务,但即使我输入了所需范围内的有效输入,仍然会收到提示。两个输入提示都会出现这种情况。我怀疑问题出在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循环:


而numberRef>=1&&numberRef也会添加您的输入和输出,预期的和实际的。首先,请尝试使用一致且清晰的缩进重新格式化代码。第二,你认为当…时你会做什么。。。;{return true;}将完成什么?这是一个无条件的回报;在循环完成后执行。我怀疑问题出在setupGame函数的while块中。为什么不分别测试代码的各个部分来确定您的怀疑是否正确?whilenumberRef<1 | | numberRef>100 | | cin.fail&&numberRef!=-1有点破了。cin.fail可以为true。读取失败,并且numberRef!=-1仍然可以允许循环退出,即使numberRef中的-1是假的。如果cin出错,您总是希望重复。看numberRef不会有什么好处。@user4581301你完全正确,-1a的输入将退出循环,就好像它只是-1一样,因为它是真正的傻瓜,需要做更多的工作,但是这是一个简单的解决方案,旨在向OP展示如何集成,而循环用于输入检查,有更好的方法吗?绝对地我会做不同的事吗?对然而,基于操作码和方法,我不认为他/她是语言方面的专家,一个复杂而深入的答案不能解释他/她目前的错误。像-1a这样的问题不值得关注。我担心读取完全失败后,numberRef会变成-1。*-1a*是有效的。程序读取-1,并在流中留下一个。另一方面,kartoffel完全失败了,如果numberRef由于任何原因结束了-1,那么while-1<1 | |-1>100 | | cin.fail&&1!=-1表示在读取失败时存在循环。首先检查cin的状态,然后才能信任numberRef中的值。
#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;
    } 
}
}