【发布时间】:2016-06-09 00:30:23
【问题描述】:
我正在开发一个程序,该程序通过输入 1-3 的整数(4 退出)提示用户从 3 个不同的选项中进行选择。我需要编写一个代码来验证输入是否为整数,如果它不是整数则重新提示它们。这是我的代码的基本思想(完整发布太长了)。
do
{
cout << "Menu: Please select one of the following options:" << endl;
cout << " 1 - Drop a single chip into one slot." << endl;
cout << " 2 - Drop multiple chips into one slot." << endl;
cout << " 3 - Drop 5 chips into each slot." << endl;
cout << " 4 - Quit the program." << endl;
cout << "Enter your selection now: ";
cin >> first_input;
}while (first_input!=4)
然后我有多个 if 语句,它们根据用户选择的选项执行表达式。稍后我还会提示他们在代码中输入其他整数值。
如果用户输入未能输入整数而是输入字符,我该如何让用户返回菜单?约束:不能使用continue或break
提前致谢。
【问题讨论】:
标签: c++ validation loops input do-while