c++ issues with cin.fail() in my program
Posted
by
Wallace
on Stack Overflow
See other posts from Stack Overflow
or by Wallace
Published on 2013-11-06T21:49:41Z
Indexed on
2013/11/06
21:53 UTC
Read the original article
Hit count: 244
cin
I want to use input y to do saving thing,and r to do resuming, but then i write it in the following codes,and then I input y or r,I just to be noticed ""Please enter two positve numbers" this line code "if(x==(int)('y'))"and next line is ignored.how could this happen
int main(){
cout<<"It's player_"<<player+1<<"'s turn please input a row and col,to save and exit,input 0,resume game input"<<endl;
while(true){
cin>>x;
if(x==(int)('y')) {save();has_saved=true;break;}
if(x==(int)('r')) {resume();has_resumed=true;break;}
cin>>y;
if(cin.fail()){
cout<<"Please enter two positve numbers"<<endl;
cin.clear();
cin.sync();}
else if (x>n||x<1||y<1||y>n) {
cout<<"your must input a positive number less or equal than "<<n<<endl;
continue;}
else if(chessboard[x][y]!=' ')
{cout<<"Wrong input please try again!"<<endl;
continue;}
else {
chessboard[x][y]=player_symbol[player+1];
break;
}
}
}
© Stack Overflow or respective owner