How to erase cin's buffer stream?

I

Immortal Nephi

The message in the console screen shows: press any key in prompt.
The string can only accept one character at this time. If key is not
valid, then message shows invalid prompt and try again. The problem
is that loop does not let you to type again and continues to copy one
character into string. The string will have two or more characters.
How do I clear or erase cin’s buffer stream?

Int main()
{
cout << “Press any key.\n”
<< “Prompt: “;

string Prompt;

cin.width( 1 );
ws( cin );
cin >> Prompt;

while( ( Prompt != “A” && Prompt != “B” ) )
{
cout << “Invalid prompt. Try again.\n\n”;
cout << “Press any key.\n”
<< “Prompt: “;

ws( cin );
cin >> Prompt;
}

cout << “Done.” << endl;

return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top