do while with character problem

Hello, i have the following code

1
2
3
4
5
6
7
8
char ans;
do
{
       cout<< "Do you want to continue (Y/N)?\n";
       cout<< "You must type a 'Y' or an 'N'.\n";
       cin >> ans;
}
while((ans !='Y')&&(ans !='N')&&(ans !='y')&&(ans !='n'));


and when i type more than one character, e.x wqe
i got output
Do you want to continue (Y/N)?
You must type a 'Y' or an 'N'.
Do you want to continue (Y/N)?
You must type a 'Y' or an 'N'.
Do you want to continue (Y/N)?
You must type a 'Y' or an 'N'.


Is it possible to just output only one time and not 3 ?

Thanks!
You can ignore all characters after the first using
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

or read a string and take only its first character:
1
2
3
4
string str;
getline(cin,str);
if (str.empty())ans=0;
else ans=str[0];
Ok, thanks again Athar !!!
Topic archived. No new replies allowed.