病例'p'||的问题'P':C ++中switch语句中的语法 [英] Problems with case 'p' || 'P': syntax within a switch statement in C++
问题描述
我已使用switch语句如下:
I've used the switch statement the following way:
switch (ch){
case 'P' || 'p':
goto balance;
break;
case 'r' || 'R':
goto menu;
break;
default:
cout<<"\t\tInvalid Choice!!"<<endl;
system ("\t\tpause");
system ("cls");
goto menu;
break;
}
但是似乎有以下语法错误:
But it seems there's something wrong with the following syntax:
case 'r' || 'R'
编译器抱怨重复大小写。
我的代码有什么问题?
Compiler complains about "duplicate case value". What's wrong with my code?
推荐答案
更改为
case 'P':
case 'p':
goto balance;
break;
使用 goto
通常不是一个好主意
Using goto
is usually not a good idea.
在原始代码中, case'P'||由于
等价于 ||
的结果是'p': case 1
code> 0 如果两个操作数都为零,或者 1
。所以在两个 case
语句中,'p'|| 'P'
和'r'|| 'R'
评估为 1
,这就是为什么您得到关于重复的值的警告。
In your original code, case 'P' || 'p':
is equivalent to case 1
as the result of ||
is 0
if both operand are zero, or 1
otherwise. So in the two case
statement, both 'p' || 'P'
and 'r' || 'R'
evaluated as 1
, that's why you got the warning about duplicate case value.
这篇关于病例'p'||的问题'P':C ++中switch语句中的语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!