如何在c ++中使用带有用户输入的枚举

Zar*_*ney 4 c++ enums

我正在制作一个简单的石头剪刀布游戏,我需要使用枚举数据结构。我的问题是我无法编译以下代码,因为从 int (userInput) 到 Throws (userThrow) 的转换无效。

enum Throws {R, P, S};
int userInput;
cout << "What is your throw : ";
cin >> userInput;
Throws userThrow = userInput;
Run Code Online (Sandbox Code Playgroud)

帮助?!

小智 5

你可以这样做:

int userInput;
std::cin >> userInput;
Throws userThrow = static_cast<Throws>(userInput);
Run Code Online (Sandbox Code Playgroud)