- #1
ChrisVer
Gold Member
- 3,378
- 465
I need some help in optimizing a code with a prompt yes/no...My problem is that if I mistype within the loop for r some character (let's say I'm missing the number 8 on the keyboard for u) the program gets into an infinite loop.
Do you know some way that I could solve this problem? Like printing "you gave a character and not an integer for r"...
Do you know some way that I could solve this problem? Like printing "you gave a character and not an integer for r"...
C:
#include<iostream>
using namespace std;
int main(){
char ans;
do{
int r=1;
while(r==1){
cout<<"you said yes"<<endl<<endl;
cout<<"are you sure?";
cin>>r;
}
cout<<"Do you want to repeat? (y/n)";
cin>>ans;
if(ans=='n' || ans=='N') {cout<<"Thanks for working with me"<<endl;}
} while(ans=='y' || ans=='Y');
system("pause");
return 0;
}
Last edited by a moderator: