while循环无限验证响应不正确的输入

时间:2014-05-01 15:15:25

标签: c++ validation loops while-loop

在案例存款部分,我已经写了一个循环来验证用户输入是一个整数,但在输入除整数之外的东西时,它会将其分配给变量信用并重复显示“请输入大于1英镑的金额”我试图在显示消息时明确输入,以便可以重复该过程,但我似乎无法清除输入错误的变量。

#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;

1 个答案:

答案 0 :(得分:1)

本声明:

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

没有做你想象的那样。它做的相当于:

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

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

您需要将cout语句移动到循环中,如下所示:

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