如何在C ++中通过用户输入使用枚举 [英] How to use enum with user input in c++

查看:102
本文介绍了如何在C ++中通过用户输入使用枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Im making a simple rock paper scissors game and I need to use the enumeration data structure. My problem is that i cannot compile the following code because of an invalid conversion from int (userInput) to Throws (userThrow).

enum Throws {R, P, S};
int userInput;
cout << "What is your throw : ";
cin >> userInput;
Throws userThrow = userInput;

帮助?!

推荐答案

R,P和S在技术上现在是数字的标识符(分别为0,1和2)。您的程序现在不知道0、1和2曾经映射到字母或字符串。

R, P and S are technically now identifiers for numbers (0,1 and 2, respectively). Your program now does not know that that 0, 1 and 2 once mapped to letters or strings.

相反,您必须获取输入并将其手动与 R进行比较, P和 S,如果匹配,请相应地设置 userThrow 变量。

Instead, you must take the input and manually compare it to "R", "P" and "S" and if it matches one, set the userThrow variable accordingly.

这篇关于如何在C ++中通过用户输入使用枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆