0

I'm just learning C++ (1 week of experience) and was trying to write an input validation loop that ask the user to enter "Yes" or "No". I figured it out but have a feeling there's a better way of approaching this. Here's what I came up with:

{
    char temp[5]; // to store the input in a string
    int test; // to be tested in the while loop

    cout << "Yes or No\n";
    cin.getline(temp, 5);

    if (!(strcmp(temp, "Yes")) || !(strcmp(temp, "No")))    // checks the string if says Yes or No
        cout << "Acceptable input";                         // displays if string is indeed Yes or No
    else                                                    //if not, intiate input validation loop
    {
        test = 0;
        while (test == 0) // loop
        {
            cout << "Invalid, try again.\n";
            cin.getline(temp, 5);               // attempts to get Yes or No again
            if (!(strcmp(temp, "Yes")) || !(strcmp(temp, "No")))  // checks the string if says Yes or No
                test = 1;         // changes test to 1 so that the loop is canceled
            else test = 0;        // keeps test at 0 so that the loop iterates and ask for a valid input again
        }
        cout << "Acceptable input";
    }

    cin.ignore();
    cin.get();

    return 0;
}

I apologize for my poor notes, not sure what is relevant. I'm also using the cstring header.

2
  • Do not use the cstring header unless you must. #include <string> is a much better solution. Commented Mar 28, 2011 at 8:08
  • 2
    Since you're working in C++, look into using the std::string class. Commented Mar 28, 2011 at 8:08

2 Answers 2

5

Even better IMO:

std::string answer;

for(;;) {
    std::cout << "Please, type Yes or No\n";
    getline(std::cin, answer);

    if (answer == "Yes" || answer == "No") break;
}

You also can transform answer into lower case that allows user to type not only "Yes", but also "yes", "yEs", etc. See this question

Sign up to request clarification or add additional context in comments.

1 Comment

Using std::string is certainly better as not only is it C++, it reduces the risk of buffer overflow and it makes string comparisons easier to read. However, this answer does not allow "no" (and its case variants) as valid input.
0

I think you want a do while loop:

bool test = false;
do 
{
   cout << "Yes or No\n";
   cin.getline(temp, 5);
   if (!(strcmp(temp, "Yes")) || !(strcmp(temp, "No"))) // checks the string if says Yes or No
   {
       cout << "Acceptable input"; // displays if string is indeed Yes or No
       test = true;
   }
   else
   {
       cout << "Invalid, try again.\n";
   }

} while (!test);

1 Comment

It was more meant to show the transformation into a do {} while loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.