C++:跳过我的程序暂停的无效条目?
新手来了!我编写了一个简单的菜单驱动程序。由于这是一项作业,因此我在完全终止程序并消失之前暂停了要求用户输入的操作。
我在菜单中使用了 switch 语句,每当用户输入有效条目时,它就会完成该条目的功能,显示“程序结束”,显示“输入任意键退出”消息并在结束之前等待输入。
但是,当我输入无效输入时,switch 语句显示默认语句,显示“程序结束”以及“输入任意键退出”。但不等待用户输入而是自行结束。
我认为不知何故,一个值被存储在我的暂停整数中,但我不明白从哪里?是留在缓冲区中的用户输入吗?
解决这个问题的最佳方法是什么?
switch (choice)
{
case 1: commFail(); break;
case 2: engFail(); break;
case 3: icing(); break;
case 4: break;
default: cout << "Invalid Entry.\n";
}
cout <<"Program ended.\n";
cout <<"Enter any key to exit.\n";
int a;
cin >> a;
return 0;
Newbie here! I have written a simple menu driven program. Since it's for an assignment, I've placed a pause asking for user input before completely terminating the program and disappearing.
I've used a switch statement for the menu and whenever the user inputs a valid entry, it completes that entrie's function, displays the "Program Ended", displays "Enter any key to exit" message and awaits input before ending.
However, when I enter invalid input, the switch statement displays the default statement, displays "Program Ended" as well as "Enter any key to exit." but does not wait for user input and instead ends on its own.
I'm thinking that somehow a value is being stored in my pause integer, but I don't understand from where? Is it the user input that's being left in the buffer?
What would be the best way to fix this?
switch (choice)
{
case 1: commFail(); break;
case 2: engFail(); break;
case 3: icing(); break;
case 4: break;
default: cout << "Invalid Entry.\n";
}
cout <<"Program ended.\n";
cout <<"Enter any key to exit.\n";
int a;
cin >> a;
return 0;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论