how do I use an enum value on a switch statement in C++

后端 未结 8 1325
独厮守ぢ
独厮守ぢ 2020-12-02 20:16

I would like to use an enum value for a switch statement. Is it possible to use the enum values enclosed in \"{}\" as cho

8条回答
  •  孤街浪徒
    2020-12-02 20:40

    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.

提交回复
热议问题