在这个开关中,无论用户输入什么,它总是跳过所有的大小写,总是显示默认的大小写!!
cout << "Enter an operator(+, -, *, /)" << endl;
cin >> oper;
cout << "Enter second number" << endl;
cin >> second;
if (second > 9999)
{
cout << "ERROR\n";
system("PAUSE");
continue;
}
switch (oper)
{
case '+':
ans = add(first, second);
case '-':
ans = subtract(first, second);
case '*':
ans = multiply(first, second);
case '/':
ans = divide(first, second);
default:
cout << "ERROR\n";
system("PAUSE");
continue;
}
最佳答案
实际上,它不会跳过案例,它会进入案例,然后执行到下一个案例,最终以默认案例结束。您需要添加 break
语句以跳出 switch
:
switch (oper)
{
case '+':
ans = add(first, second);
break;
case '-':
ans = subtract(first, second);
break;
case '*':
ans = multiply(first, second);
break;
case '/':
ans = divide(first, second);
break;
default:
cout << "ERROR\n";
system("PAUSE");
continue;
}
关于c++ - switch 语句直接转到 default,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26813235/