Problems with case #39;p#39; || #39;P#39;: syntax within a switch statement in C++(案例“p的问题||P:C++ 中 switch 语句中的语法)
问题描述
我使用 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<<" Invalid Choice!!"<<endl;
system (" pause");
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,因为如果两个操作数都为零,则 || 的结果是 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 语句中的语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:案例“p"的问题||'P':C++ 中 switch 语句中的语法
基础教程推荐
- 初始化列表*参数*评估顺序 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- CString 到 char* 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
