c++输入验证打印两次
c++ Input Validation printing twice?
不知道为什么要打印两次。这只是练习,我试图使用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;
}
相关文章:
- 在指定的响应之前C++用户输入两次
- 将用户输入验证为整数:用户必须两次输入输入
- C++使用二维数组的程序,该数组要求用户输入两次.它需要将这些输入显示为输出
- 如何修复密码条目,它一次输入两个字符
- 为什么输入文件的最后一行运行两次?
- cin.ignore() 输入两次以获得一次暂停
- 为什么在这里对c++输入文件流进行两次检查
- C 必须将输入输入两次
- 重复上一次输入两次
- C++程序要求我输入两次,即使变量第一次被分配
- c ++ 如何阻止我的循环两次吐出最后一个输入
- 函数读取输入并运行函数两次
- 为什么我必须输入两次
- 如何在获得下一个字符后正确刷新输入缓冲区,而无需按两次回车键!C++
- 为什么 ifstream 返回输入文件的最后一个值两次
- 尝试从输入文件打印,但打印最后一个输入两次
- 为什么我的代码要求"请输入 2-12 之间的数字:"两次?
- 两次读取std::cin的输入
- 如何阻止用户两次输入相同的值
- Boost接受器每次接受两次输入连接,并生成两个线程