0

I'm trying to do one check for invalid input data type. If the the input data type is of type char, I want the re-loop over the menu options. But my program terminates instead.

int menu()
{
    int choice = 15;
    while ((choice > 14) || ( choice < 0))
    {
        cout << "Enter 0 to quit\n";
        cout << "Enter 1 for Addition\n";
        cout << "Enter 2 for Subtraction\n";
        cout << "Enter 3 for Multiplication\n";
        cout << "Enter 4 for Division of two integers\n";
        cout << "Enter 5 for Real Division of two integers\n";
        cout << "Enter 6 for Quotient of a division\n";
        cout << "Enter 7 for Remainder of a division\n";
        cout << "Enter 8 for Factorial of an integer\n";
        cout << "Enter 9 for Exponential of two integers\n";
        cout << "Enter 10 for Finding if number is even or odd\n";
        cout << "Enter 11 for Area of a Square\n";
        cout << "Enter 12 for Area of a Circle\n";
        cout << "Enter 13 for Area of an Isoceles Triangle\n";
        cout << "Enter 14 for Converting Decimal to binary or hexadecimal\n";
        cin >> choice;

        if((choice > 14) || (choice < 0))
        {
            cout << "Invalid entry: Try again" << endl << endl;
        }
        else if ( !choice ) 
        {
            return choice;
        }
        else if (choice)
        {
            return choice;
        }
    }

    return choice;
}

Output after entering char 'f' as cin

Dean Seo
  • 5,486
  • 3
  • 30
  • 49
Rudy V
  • 21
  • 5
  • use "try" and "catch" clause, use the exception you get when You type invalid character – Rafalsonn Dec 07 '17 at 06:27
  • I don't know how. – Rudy V Dec 07 '17 at 06:29
  • write the exception You get in the console and I think I can help – Rafalsonn Dec 07 '17 at 06:30
  • Use `cin.fail()` https://stackoverflow.com/questions/17928865/correct-way-to-use-cin-fail – Sniper Dec 07 '17 at 06:31
  • Debug your program, check which return statement is being executed, and change it to handle the case that you wish to avoid returning from (BTW, you would probably want to initialize `choice` to some sort of "illegal" value before you scan it from the user). – goodvibration Dec 07 '17 at 06:33
  • @Sniper with cin.fail() it quits the program automatically tho right? I wan't to reloop tho. – Rudy V Dec 07 '17 at 07:09
  • Nope. `cin.fail()` check if input is correct. You could do `if(cin.fail) continue;` Or something like that – Sniper Dec 07 '17 at 07:14

2 Answers2

0

Try this instead

if (cin >> choice)
{
    if((choice > 14) || (choice < 0))
        cout << "Invalid entry: Try again" << endl << endl;
    else
        return choice;
}
else //Fail to cin into choice, user input is not a number
    cout << "Invalid entry: Please key in a number." << endl;

Also, the while condition should change to while (true)

lamandy
  • 962
  • 1
  • 5
  • 13
0

Remove space line and then run the code again.

if((choice > 14) || (choice < 0))
    cout << "Invalid entry: Try again" << endl << endl;

else if ( !choice ) 
{
 return choice;
}
////// remove the empty space line below///////////
else if (choice)
    return choice;
///////////////////////////////////////////////////
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770