user3560533
user3560533

Reputation: 31

while loop infinite validation response to incorrect input

In the case deposit section i have written a loop to validate that the user input is a integer but upon entering something other than an integer it assigns that to the variable credit and repeatedly displays "Please enter an amount greater than £1" i have tried to make it clear the input upon displaying the message so the process can be repeated but i cannot seem to get it to clear the variable of the incorrect input.

#include <iostream>
#include <string>

using namespace std;

enum Menusystem // enum is a function used to convert the list of words below into numbers.
{
    ShowMenu, EnterName, Account, Deposit, Withdraw, Balance, Exit

} menu;

int main()

{

string customer; // declaring variables and their data types for the menu options.
string accounttype;
string name;
int credit;
int debit;
int currentbal;

bool exit = false; // declaring variable as boolean setting it to false so that when the loop gets to false it will break the loop and exit the program.
int option = 0; // declares showmenu as a integer and sets value to 0 so that the menu will be displayed upon opening the program as in the code below the menu is displayed if option is equal to 0.

while (!exit) // initiates a while loop so that when the loop gets to false it will break the loop and exit the program.
{
    switch (menu) // initiates a switch statement which will allow the program to cycle through menu options depending on the menu option the user selects.
    {
    case ShowMenu:

       cout << "[0] - Show menu\n" // displays menu options to user.
            << "[1] - Enter Full Name\n"
            << "[2] - Enter Account Type\n"
            << "[3] - Deposit Funds\n"
            << "[4] - Withdraw Funds\n"
            << "[5] - Display Balance\n"
            << "[6] - Exit Program\n";

        cin >> option;
        if (option == 0) menu = ShowMenu; 
        else if (option == 1) menu = EnterName;
        else if (option == 2) menu = Account;
        else if (option == 3) menu = Deposit;
        else if (option == 4) menu = Withdraw;
        else if (option == 5) menu = Balance;
        else if (option == 6) menu = Exit;
        else menu = ShowMenu; // default case is to show the menu.

        break;

    case EnterName:

        system("CLS");

        cout << "Please enter your full name >\n";
        cin >> customer;

        system("CLS");

        menu = ShowMenu;

        break;

    case Account:



        menu = ShowMenu;

        break;

    case Deposit:

        system("CLS");

        cout << "Please enter an amount you wish to deposit >\n";
        cin >> credit;

        while (!(cin >> credit)) cout << "Please enter an amount greater than £1";
        {
            cin.clear();
            cin.ignore(100, '\n');
        }

        system("CLS");

        menu = ShowMenu;

        break;

    case Withdraw:

        system("CLS");

        cout << "Please enter an amount you wish to withdraw >\n";
        cin >> debit;

        system("CLS");

        menu = ShowMenu;

        break;

    case Balance:

        cout << credit;
        cout << customer;

        break;

    case Exit:



        break;

    default:
        break;
    }
}

return 0;

Upvotes: 0

Views: 264

Answers (1)

Rose Kunkel
Rose Kunkel

Reputation: 3268

This statement:

while (!(cin >> credit)) cout << "Please enter an amount greater than £1";
{
    cin.clear();
    cin.ignore(100, '\n');
}

is not doing what you think it is. It's doing the equivalent of this:

while (!(cin >> credit)) {
    cout << "Please enter an amount greater than £1";
}

    cin.clear();
    cin.ignore(100, '\n');

You need to move the cout statement into the loop, like this:

while (!(cin >> credit)) {
    cout << "Please enter an amount greater than £1";
    cin.clear();
    cin.ignore(100, '\n');
}

Upvotes: 1

Related Questions