Restricting a prompt

I'm new here, hello everybody.

What is the most common way to prompt a user for a choice (eg. to type y/n) so that the program allows only y or n chars to be typed in and disallow any other characters? Also, going back to the prompt if a bad char is typed in.

TIA
Use a loop
eg:
1
2
3
4
5
6
7
8
9
10
11
12
13
char resp;
while (true)
{
    cout << "Do you want to do something? (y|n) ";
    cin >> resp;
    if (resp=='n') break;
    if (resp=='y') 
    {
        /*do something*/ 
        break;
    }
    cout << "\n\nInvalid response!\n";
}
Thanx
Topic archived. No new replies allowed.