如何在C ++中的switch语句上使用枚举值 [英] how do I use an enum value on a switch statement in C++
问题描述
我想为 例如: 您可以使用枚举值就像一个整数: I would like to use an For example:
You can use an enumerated value just like an integer:
这篇关于如何在C ++中的switch语句上使用枚举值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!开关
语句使用枚举
值。可以使用{}
中包含的枚举值作为
的选项, switch()
?我知道 switch()
需要一个 int
为了将编程流程引导到适当的 case
号码,如果是这种情况,我只需为枚举中的每个常量创建一个变量
语句?
我还希望用户能够选择选项,并将该选项传递给 switch()
语句。 / p>
cout<<1 - ,;
cout<2 - Medium;
cout<3 - Hard:;
枚举myChoice {EASY = MEDIUM = 2,HARD = 3};
cin> ????
开关(????)
{
case 1 / EASY://(我可以输入case EASY吗?)
cout<你选择容易!;
break;
case 2 / MEDIUM :
cout<你选中了!;
break;
case 3 / HARD:// .....(同样的事情,除了硬的情况2)
默认值:
返回0;
}
myChoice c;
...
开关(c){
case EASY:
DoStuff();
break;
case MEDIUM:
...
}
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. 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: // ..... (same thing as case 2 except on hard.)
default:
return 0;
}
myChoice c;
...
switch( c ) {
case EASY:
DoStuff();
break;
case MEDIUM:
...
}