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
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 从 std::cin 读取密码 2021-01-01