C++ 循环或编码用户错误的输入,直到正确的输入c++; num1; cin>>num2; 总计=加(num1,num2); coutnum2; 总计=减去(num1,num2); coutnum2; 总计=乘以(num1,num2); coutnum2; 总计=除以(num1,num2); cout
您可以使用goto作为基本,因此当用户输入除1、2、3或4以外的任何数字时,她会要求她再次输入数字。但请记住,在良好的编码实践中不建议使用goto。它为您的简单问题提供了一个简单的解决方案C++ 循环或编码用户错误的输入,直到正确的输入c++; num1; cin>>num2; 总计=加(num1,num2); coutnum2; 总计=减去(num1,num2); coutnum2; 总计=乘以(num1,num2); coutnum2; 总计=除以(num1,num2); cout,c++,calculator,C++,Calculator,您可以使用goto作为基本,因此当用户输入除1、2、3或4以外的任何数字时,她会要求她再次输入数字。但请记住,在良好的编码实践中不建议使用goto。它为您的简单问题提供了一个简单的解决方案 int UI() { int choice; x: cout << "Enter the correspendonce number for the following options you want "
int UI() {
int choice;
x:
cout << "Enter the correspendonce number for the following options you want "
": \n1.Addition\n2.Subtraction\n3.Multiplication\n4.Division\n\n "
"Option: ";
cin >> choice;
switch (choice)
{
case 1:
case 2:
case 3:
case 4:
return choice;
default:
cout << "Please enter 1&2&3 or 4\n";
goto x;
}
}
intui(){
智力选择;
x:
选择;
开关(选择)
{
案例1:
案例2:
案例3:
案例4:
回报选择;
违约:
不能用下面的代码替换您的代码。
简单的解决方案是将要在while循环中重复的代码包装起来
只有当用户输入正确的输入时,循环才会结束,否则循环将重复,使用“break”结束循环
intmain(){
整数合计;
int num1,num2;
智力选择;
而(1)
{
choice=UI();
如果(选项==1){
cout>num1;
cin>>num2;
总计=加(num1,num2);
coutnum2;
总计=减去(num1,num2);
coutnum2;
总计=乘以(num1,num2);
coutnum2;
总计=除以(num1,num2);
cout一个非常简单的方法是设置一个标志:
bool done = false;
while (!done)
{
done = true;
int choice = UI();
if (choice == 1) {
// etc ...
} else {
cout << "Entered wrong input, please select option 1 - 4\n";
done = false;
}
}
问题是,如果用户输入hello
或任何非整数,您将得到一个错误。使用基于行的输入解决此问题的最简单方法是:
// Note, you also need to include <string> and <sstream>
int GetChoice(int count)
{
string input;
while (getline(cin, input))
{
int choice;
istringstream iss(input);
if (input >> choice) {
if (choice >= 1 && choice <= count) {
return choice;
}
}
cout << "Invalid choice. Enter value from 1 to " << count << endl;
}
return 0;
}
//注意,您还需要包括和
int GetChoice(int计数)
{
字符串输入;
while(getline(cin,输入))
{
智力选择;
istringstream iss(输入);
如果(输入>>选项){
如果(choice>=1&&choice别这样,现在不是1989年……用goto
解决这个问题是应该受到谴责的建议。@帕迪,我完全同意你的观点,使用goto是不好的,我在回答时也说过这一点。如果我们看看这个问题,问这个问题的朋友是非常基本的。我的目标是在不创建syn的情况下为他提供一个简单的解决方案对人群征税并使其难以理解。当然,否则使用goto是不好的。欢迎您这样证明它的合理性,但请记住,您已经通过使用switch引入了新语法,并推荐了一种根本不常见的解决方案。很像“简单解决方案”因为有人撞到你的脸,是为了杀死他们,而不是谈判。这可能是一个解决办法,但这并不是一个常态,只是更多问题的开始。@帕迪谢谢你分享你的宝贵经验。从现在起,我会考虑你在帮助别人时所说的话。你改变了什么?为什么?一个好的答案应该解释T。提示并帮助人们更好地理解该语言。仅仅提供代码将导致cargo cult编程。@churill我理解了你说的话,我编辑了我的答案。这引入了错误的可能性,因为代码重复——对代码的任何更改都必须认识到,break
对于防止错误行为至关重要r(例如,如果添加新选项)。虽然在这样一个简单的示例中识别和测试这一点很简单,但始终要记住这一点,因为有一天您将在一个复杂得多的环境中编写代码,使行为尽可能明显是有益的。非常感谢您,这真的很有帮助,我的代码正在顺利运行!
// Gets 1-based choice up to count. Returns 0 on error.
int GetChoice(int count)
{
while (cin >> choice)
{
if (choice >= 1 && choice <= count) {
return choice;
}
cout << "Invalid choice. Enter value from 1 to " << count << endl;
}
return 0;
}
int choice = GetChoice(4);
// Note, you also need to include <string> and <sstream>
int GetChoice(int count)
{
string input;
while (getline(cin, input))
{
int choice;
istringstream iss(input);
if (input >> choice) {
if (choice >= 1 && choice <= count) {
return choice;
}
}
cout << "Invalid choice. Enter value from 1 to " << count << endl;
}
return 0;
}