Home > Software engineering >  How to check if input is valid and keep asking for input if it's not
How to check if input is valid and keep asking for input if it's not

Time:12-14

Can anybody please explain why this while loop is not working properly?

#include <iostream>
using namespace std; 
void ask_user(int a, int b){
    char choice = ' '; 
    while(choice != 'q' && choice != 'a' && choice != 's' && choice != 'm'){
        cout << "choose operation" << endl; 
        cout << "a to add, s to subtract, m to multiply and q to quit" << endl; 
        cout << "----------------------------" << endl;
        cin >> choice;
        switch(choice){
            case 'a' : cout << "a   b = " << a   b; 
            break;
            case 's' : cout << "a - b = " << a   b; 
            break;
            case 'm' : cout << "a * b = " << a   b;
            break;
            case 'q' : break; 
            default: cout << "please Enter a valid choice " << endl;
        }
    }
}

int main(){
    ask_user(7, 9); 
    return 0; 
}

If I enter p for exemple which is not valid then it works fine and asks for valid input again,

but if I enter pp that's when it starts bugging and prints the message twice. If I enter ppp it

prints three times etc...

CodePudding user response:

First thing, you have a misunderstanding of how switch works. Each case must end with break statement so that the following one won't get executed.
Which means that a break will break the switch, not the while.

But the main issue is that the logic of your program is wrong.
You should not loop over the validity of the given input, let the switch statement handle that in the default clause.

Instead you should loop over a flag that will be set when the user press the q key.

So considering you have the following functions defined to respectively display the menu and ask for the operands to operate on (defined for code readability):

void display_menu(char & choice)
{
    std::cout << "Operation:\na: Addition\nm: Multiplication\ns: Substraction\nq: Quit\n";
    std::cin >> choice;
}
void ask_operands(int & a, int & b)
{
    std::cout << "\na ?";
    std::cin >> a;
    std::cout << "\nb ?";
    std::cin >> b;
    std::cout << '\n';
}

The logic of your code can be then rewritten as:

int main()
{
    bool quit = false;
    char choice;
    int a, b;

    ask_operands(a, b); // Ask the user which operands to use
    while(!quit)        // loop over the flag
    {
        display_menu(choice);

        switch(choice)
        {
            case 'a': std::cout << (a b);
                      break;
            case 'm': std::cout << (a*b);
                      break;
            case 's': std::cout << (a-b);
                      break;
            case 'q': std::cout << "Exiting...";
                      quit = true; // Set the flag to false
                      break;
            default: std::cout << "Invalid choice, try again."; //Here you handle the invalid choices (i.e. let the loop iterate again)
        }
        std::cout << '\n';
    }

    return 0;
}

Live example

Note: If you want the user to be able to change the value of the operands at each iteration, just move the ask_operands(a, b); call inside the loop.

  • Related