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

标签 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:

最佳答案

您忘记再次读取输入:

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

关于C++ 输入验证打印两次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33120173/

相关文章:

c++ - 为什么在头文件中定义类有效但函数无效

c++ - GCC : sorry, 未实现:重整过载

c++ - 为什么不区分大小写与 std::unordered_set 的 std::hash 函数一起使用?

c++ - dev-cpp 和 Microsoft Visual C++ math.h 的区别

c++ - 在VS Code调试器中更好地打印2d数组-C++

c++ - 可重载的 boost::asio::basic_stream_socket

c++ - clang 4.0.0 cuda 和 cmake 最小警告的最佳实践

c++ - 可以从并行线程完成对 boost 的 rtree 的查询吗?

c++ - 在 `std::get<I>` 上使用 `std::tuple` 是否保证对于 `I` 的不同值是线程安全的?

c++ - Qt:恢复 QTextEdit 的几何形状失败