c ++输入验证打印两次?

时间:2015-10-14 08:21:58

标签: c++ c++11

不知道为什么打印两次。这只是练习我尝试使用cin.clear()/ cin.ignore。尝试在switch语句中编写包含函数的菜单。

#include <iostream>
#include <iomanip>
using namespace std;

void showMenu();
void showFees(double, int);

int main()
{
    int choice;
    int months;

    const int ADULT_CHOICE = 1;
    const int CHILD_CHOICE = 2;
    const int SENIOR_CHOICE = 3;
    const int QUIT_CHOICE = 4;

    const double ADULT = 40.0;
    const double CHILD = 20.0;
    const double SENIOR = 30.0;

    do
    {
        showMenu();
        cin >> choice;

        while (choice < ADULT_CHOICE || choice > QUIT_CHOICE)
        {
            cout << "Please enter a valid menu choice: ";
            cin.clear();
            cin.ignore(INT_MAX, '\n');
        }



        if (choice != QUIT_CHOICE)
        {
            cout << "For how many months? ";
            cin >> months;

            switch (choice)
            {
            case ADULT_CHOICE:
                showFees(ADULT, months);
                break;
            case CHILD_CHOICE:
                showFees(CHILD, months);
                break;
            case SENIOR_CHOICE:
                showFees(SENIOR, months);
                break;
            }

        }

    } while (choice != QUIT_CHOICE);

    system("pause");
    return 0;
}

void showMenu()
{
    cout << "\nHealth Club Membership Menu" << endl << endl;
    cout << "1. Standard Adult Membership" << endl;
    cout << "2. Child Membership" << endl;
    cout << "3. Senior Citizen Membership" << endl;
    cout << "4. Quit the Program" << endl << endl;
    cout << "Enter your choice: ";
}

void showFees(double memberRate, int months)
{
    cout << "The total charges are $" << (memberRate * months) << endl;
}

这就是它的印刷品。我是一个相当新的c ++,所以我不确定是什么错。是间距吗?毫无头绪。

Health Club Membership Menu

1. Standard Adult Membership
2. Child Membership
3. Senior Citizen Membership
4. Quit the Program

Enter your choice: 0
Please enter a valid menu choice: Please enter a valid menu choice: 0
Please enter a valid menu choice:

3 个答案:

答案 0 :(得分:1)

您忘记再次阅读输入:

master

您只读了一次输入。然后你进入while检查选择,你做cin.clear()和cin.ignore()再次你的循环执行,因为没有新的数据传递:这应该做的工作:

cin >> choice;

        while (choice < ADULT_CHOICE || choice > QUIT_CHOICE)
        {
            cout << "Please enter a valid menu choice: ";
            cin.clear();
            cin.ignore(INT_MAX, '\n');
        }

答案 1 :(得分:1)

输入流如下所示:

0\n

您首先阅读0,现在流

\n

0无效,因此您打印"Please enter a valid menu choice: "并跳过换行符 流现在是空的。

由于choice的值未发生变化,因此它仍然无效,您再次打印"Please enter a valid menu choice: "

这次输入流是空的,因此ignore必须等待下一个换行符,这就是你第一次只获得两个输出的原因。

如果您在完成输入处理后完成了输出:

cin.clear();
cin.ignore(INT_MAX, '\n');
cout << "Please enter a valid menu choice: ";

输出没有重复。

另一个错误是即使您输入了有效值,循环也永远不会退出。

原因是您从未读过choice的新值。

您需要添加

cin >> choice;

在验证循环结束时。

答案 2 :(得分:0)

问题出在这里:

  while (choice < ADULT_CHOICE || choice > QUIT_CHOICE)
        {
            cout << "Please enter a valid menu choice: ";
            cin.clear();
            cin.ignore(INT_MAX, '\n');
        }

试试这段代码。您必须再次向变量choice读取值。

  while (choice < ADULT_CHOICE || choice > QUIT_CHOICE)
        {
            cout << "Please enter a valid menu choice: " << endl;
            cin >> choice;
            cout << endl;
        }