输入有效的输入值后仍会提示

时间:2018-12-31 05:05:40

标签: c++

我这样做是为了分配作业,但是即使我输入了所需范围内的有效输入,我仍然会得到提示。这两种输入提示都在发生这种情况。我怀疑问题出在while函数的setupGame块中。

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

1 个答案:

答案 0 :(得分:0)

您正在与这些嵌套循环纠缠不清。您的提示继续打印,因为while循环:

while(numberRef >=1 && numberRef <=100)

将继续重复其在do之前的块,直到您输入的值小于1或大于100,最后一个while循环也是如此。

我假设您正在使用cin.clear()刷新先前的输入,如果要停止它,则不是cin.clear()的目的。相反,它用于清除cin的错误状态。请仔细阅读here

下面是实现所需功能的代码,请观察我在每个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;
    } 
}
}