How to validate if a user entered "Y" or "N" as an answer and if not, notify the user to enter again in C++?

2 Answers

Relevance
  • 5 months ago

    as with all C based languages most people new to it would use keyboard input that is record based and waits for you to press the Return key. if you want character input then you need to remember you are responsible for taking over the monitoring for all things a person might press on the keyboard eg DEL, RETurn,TAB,SPACE, and other keys that return two codes such as F-keys. So for C you would use the KBHIT() function instead and it starts getting complicated. But that may not be possible in C++ because its still using record input eg like a line of text from a file. So the user would have to press Y followed by RETurn before the Y was read in. what you need to learn to do is debug your programs by adding a Print statement after the reading of the single character(if thats how you would like it to work) eg "Y"to echo back what string you actually received so you can see the effects of record input for yourself and how two code characters can mess you up a bit. So you may want to practice like the example below.

    • Log in to reply to the answers
  • 5 months ago

    In C++ and most other languages, use a while loop to repeat something until you get the result you're looking for. One way to do that is to create a Boolean ("true/false") variable that gets set to true only when you find what you're after.

    bool have_yn = false; // haven't seen the Y or N

    std::string answer; // holds the user's response

    while (! have_yn)

    {

    .... std::cout << "Is it safe? ";

    .... std::cin >> answer;

    .... have_yn = (answer == "Y") || (answer == "N"); // true if Y or N received

    }

    That idea always works, and prevents testing for the condition more than once per loop. You can use that in a statement added to the end of the loop, just before the } brace:

    .... if (!have_yn)

    .... .... std::cout << "Please answer Y or N!" << std::endl;

    That's perfectly "structured", so if you have a boss or instructor who doesn't like needless break and continue statements, this will work.

    The disadvantage is that you have that boolean variable still hanging around after you're done with it. This is where using a break; statement can make the program simpler. Or simpler at least to someone who knows what break; does.

    std::string answer;

    while (true)

    {

    .... std::cout << "Is it safe? ";

    .... std::cin >> answer;

    .... if (answer == "Y" || answer == "N")

    .... .... break;

    .... std::cout << "Please answer Y or N!" << std::endl;

    }

    The while() condition is a constant (true), so the only way to get out of the loop is for that break; statement to be executed. A break immediately exits the innermost surrounding loop (or switch) statement.

    Something to watch out for, particularly from C programmers who've moved to C++, is writing the "while (true)" loop as

    for(;;){

    .... loop body

    }

    If the loop test in a for statement (the middle expression beteween ; semicolons) is missing, it's assumed to be (true), so that statement says exactly the same thing as "while (true)". Use it or not, depending on your taste and programming standards; but you'll see it in code from others even if you don't like it.

    • Log in to reply to the answers
Still have questions? Get answers by asking now.