【发布时间】:2016-11-26 16:41:43
【问题描述】:
作为一个初学者,我遇到了一件相当困难的事情......我想用随机生成的信息填充这个结构
struct bunny {
bool is_vampire;
std::string name;
std::string color;
char gender;
int age;
int birth_date;
};
例如,我想使用枚举
enum colors{white, brown, black, spotted};
当我初始化一些结构时,我会创建一个函数
void struct_fill(bunny x)
{
x.is_vampire = rand()%2;
x.color = colors(rand() % 11);
..... etc.
}
但我认为如果我使用一个数字,它会返回该数字所代表的值 /如果可以说数字2是黑色的/ ...有什么简单的解决方案吗?或者用从一些选择中随机选择的数据填充结构的其他方式? *我为我的英语道歉
【问题讨论】:
-
您看到错误消息了吗?
-
由于
color是std::string类型,你不能用枚举来做到这一点。您将需要std::vector<std::string>之类的东西并随机选择一个索引。 -
哦,好吧,我想我误解了为什么存在枚举并以它们的用途使用它,谢谢
-
您是否希望将
enum转换为文本? -
您可以将
color的类型从string更改为colors。