c++输入验证打印两次

c++ Input Validation printing twice?

本文关键字:两次 输入 验证 打印 c++      更新时间:2023-10-16

不知道为什么要打印两次。这只是练习,我试图使用cin.clear()/cin。忽略。尝试在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:

您又忘记读取输入了:

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

你读一次输入。然后进入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');
            cin >> choice;
        }

输入流如下所示:

0n

首先读取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;

问题在这里:

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