0

I am trying to build a menu on console on C++ with CodeBlock. Is for a course.

I would like to validate garbage enter by the user. For example, the user have to enter a number. If he enters a wrong number, no problem, the program work and continue. But if He enters a letter or some garbage, the program start on infinite loop.

I cannot use system(PAUSE) because I am programming on Linux. I tried some code like cin.get() or do while with cin.get() but no result.

Here is my code :

 #include <iostream>
    
    using namespace std;
    
    void showMenu()
    {
        cout << "---------------MENU --------------" << endl;
        cout << "1- Check balance :" << endl;
        cout << "2- Check deposit :" << endl;
        cout << "3- Withdraw:" << endl;
        cout << "4- Exit" << endl;
        cout << "--------------------------------" << endl;
    }
    
    int main()
    {
        int option;
        double balance = 500;
    
        do
        {
            showMenu();
            cout << "Option: ";
            cin >> option;
            cout << "\033[2J\033[1;1H";
    
            switch(option)
            {
               case 1:
                 cout << "Balance is: " << balance << " $" << endl;
                 break;
    
               case 2:
                 cout << "Deposit amount: " << endl;
                 double depositAmount;
                 cin >> depositAmount;
                 balance += depositAmount;
                 break;
    
               case 3:
                 cout << "Withdraw amount: " << endl;
                 double withdrawAmount;
                 cin >> withdrawAmount;
    
                 if (withdrawAmount <= balance) {
                     balance -= withdrawAmount;
                 }
                 else {
                    cout << "Not enough money" << endl;
                 }
                 break;
    
               default:
                  cout << "Your choice is invalid ";
                  do {
                    cout << '\n' << "Press the Enter key to continue.";
                  }while (cin.get() != '\n');
    
            }
            
        } while(option != 4);
    
        return 0;
    }

Do you have an idea how can I validate easily the garbage enter by the user ?

Thank you for your Help

Joc380
  • 25
  • 7
  • If the input is outright garbage, `cin >> option;` will likely fail. If you use `if (cin >> option)` , you can use an `else` to clean up the mess the user left, clearing the error flags with the `clear` method and removing the bad input with `ignore`. – user4581301 Sep 07 '21 at 21:38
  • Typically I will write a separate function that gets an integer from the user and won't return until I know I got a valid integer in the correct range. You can use this function anywhere you need to get an integer and save yourself from code repetition and wasted time. – user4581301 Sep 07 '21 at 21:40
  • 1
    Taking the input as a string and testing for a successful conversion is another method. I second putting all of this into a function. – sweenish Sep 07 '21 at 21:41
  • Thank you so much ! I place cin.clear() and cin.ignore() in the default part. It works ! I tried with a function, but no success. – Joc380 Sep 07 '21 at 23:26

1 Answers1

0

It works with the code on default part : cin.clear and cin.ignore(). This last one is important. Thank you to user4581301

#include <iostream>

using namespace std;

void showMenu()
{
    cout << "---------------MENU --------------" << endl;
    cout << "1- Check balance :" << endl;
    cout << "2- Check deposit :" << endl;
    cout << "3- Withdraw:" << endl;
    cout << "4- Exit" << endl;
    cout << "--------------------------------" << endl;
}


int main()
{
    int option;
    double balance = 500;

    do
    {
        showMenu();
        cout << "Option: ";
        cin >> option;
        cout << "\033[2J\033[1;1H";

        switch(option)
        {
           case 1:
             cout << "Balance is: " << balance << " $" << endl;
             break;

           case 2:
             cout << "Deposit amount: " << endl;
             double depositAmount;
             cin >> depositAmount;
             balance += depositAmount;
             break;

           case 3:
             cout << "Withdraw amount: " << endl;
             double withdrawAmount;
             cin >> withdrawAmount;

             if (withdrawAmount <= balance) {
                 balance -= withdrawAmount;
             }
             else {
                cout << "Not enough money" << endl;
             }
             break;

           default:

              cout << "\033[2J\033[1;1H"; //for clean screen with Linux
              cout << "Your choice is invalid " << endl;
              cin.clear();
              cin.ignore();
            
        }
        

    } while(option != 4);

    return 0;
}
Joc380
  • 25
  • 7
  • A note about `ignore()`. This only removes one character from the stream. A real jerk will type in something like *banana* and trigger the error 6 times. You can remove everything the user typed on the current line with [`cin.ignore(numeric_limits::max(), '\n')`](https://stackoverflow.com/questions/25020129/cin-ignorenumeric-limitsstreamsizemax-n) – user4581301 Sep 07 '21 at 23:42