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