如何在 C++ 的 switch 语句中使用枚举值?
我想在 switch
语句中使用 enum
值。是否可以使用 "{}"
中包含的 enum
值作为 switch()
" 的选择?
我知道 switch()
需要一个 int
eger 值,以便将编程流程引导到适当的 case
编号。如果是这种情况,我是否只需执行此操作即可。 enum
语句中的每个常量都有一个变量?
我还希望用户能够选择选项并将该选项传递给 switch()
语句,
例如:
cout << "1 - Easy, ";
cout << "2 - Medium, ";
cout << "3 - Hard: ";
enum myChoice { EASY = 1, MEDIUM = 2, HARD = 3 };
cin >> ????
switch(????)
{
case 1/EASY: // (can I just type case EASY?)
cout << "You picked easy!";
break;
case 2/MEDIUM:
cout << "You picked medium!";
break;
case 3/HARD: // ..... (the same thing as case 2 except on hard.)
default:
return 0;
}
I would like to use an enum
value for a switch
statement. Is it possible to use the enum
values enclosed in "{}"
as choices for the switch()
"?
I know that switch()
needs an int
eger value in order to direct the flow of programming to the appropriate case
number. If this is the case, do I just make a variable for each constant in the enum
statement?
I also want the user to be able to pick the choice and pass that choice to the switch()
statement.
For example:
cout << "1 - Easy, ";
cout << "2 - Medium, ";
cout << "3 - Hard: ";
enum myChoice { EASY = 1, MEDIUM = 2, HARD = 3 };
cin >> ????
switch(????)
{
case 1/EASY: // (can I just type case EASY?)
cout << "You picked easy!";
break;
case 2/MEDIUM:
cout << "You picked medium!";
break;
case 3/HARD: // ..... (the same thing as case 2 except on hard.)
default:
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您可以将 enum 与 int 进行转换。
You can cast enum with int.
您可以使用
std::map
将输入映射到enum
:You can use a
std::map
to map the input to yourenum
:我在使用带有开关盒的枚举时遇到了类似的问题。
后来我自己解决了......下面是更正后的代码,也许这可能会有所帮助。
I had a similar issue using enum with switch cases.
Later, I resolved it on my own....below is the corrected code, and perhaps this might help.
您应该记住,如果您从另一个函数访问类范围的枚举,即使它是友元,您也需要提供带有类名的值:
注意它是如何
PlayingCard::HEARTS
而不仅仅是HEARTS
。You should keep in mind that if you are accessing a class-wide enum from another function, even if it is a friend, you need to provide values with a class name:
Note how it is
PlayingCard::HEARTS
and not justHEARTS
.用户的输入始终以字符串的形式提供给您...如果您想将用户的输入从字符串转换为整数,您需要提供代码来执行此操作。如果用户输入一个数字(例如“1”),则可以将字符串传递给atoi()以获取该字符串对应的整数。如果用户输入英文字符串(例如“EASY”),那么您需要检查该字符串(例如使用 strcmp()),并根据检查匹配的情况将适当的整数值分配给您的变量。一旦获得了从用户输入字符串派生的整数值,就可以像往常一样将其传递到 switch() 语句中。
The user's input will always be given to you in the form of a string of characters... if you want to convert the user's input from a string to an integer, you'll need to supply the code to do that. If the user types in a number (e.g. "1"), you can pass the string to atoi() to get the integer corresponding to the string. If the user types in an english string (e.g. "EASY") then you'll need to check for that string (e.g. with strcmp()) and assign the appropriate integer value to your variable based on which check matches. Once you have an integer value that was derived from the user's input string, you can pass it into the switch() statement as usual.
您可以像使用整数一样使用枚举值:
You can use an enumerated value just like an integer:
你走在正确的轨道上。您可以将用户输入读取为整数并对其进行
切换
:You're on the right track. You may read the user input into an integer and
switch
on that:需要注意的一些事情:
您应该始终在名称空间内声明枚举,因为枚举不是正确的名称空间,您会很想像使用它们一样使用它们。
始终在每个 switch 子句的末尾有一个中断,否则执行将继续向下直到结束。
始终在开关中包含
default:
大小写。为了清楚起见,使用枚举类型的变量来保存枚举值。
有关 C++ 中枚举的正确使用的讨论,请参阅此处。
这就是您想要做的。
Some things to note:
You should always declare your enum inside a namespace as enums are not proper namespaces and you will be tempted to use them like one.
Always have a break at the end of each switch clause execution will continue downwards to the end otherwise.
Always include the
default:
case in your switch.Use variables of enum type to hold enum values for clarity.
see here for a discussion of the correct use of enums in C++.
This is what you want to do.