Choosing random from enum
Apr 30, 2015 at 3:29pm
Hello. How do I make this work?
1 2 3 4 5 6 7 8
|
enum colors {white=0,brown,black,spotted};
int main()
{
srand(time(0));
string test;
test = (string)(rand() % 4);
}
|
Apr 30, 2015 at 3:35pm
you have no strings :(
1 2 3 4 5 6 7 8 9
|
enum colors {white=0,brown,black,spotted,num_colours};
string s_colours[] = {"white","brown","black","spotted"};
int main()
{
srand(time(0));
string test;
test = s_colours[rand() % num_colours];
}
|
Apr 30, 2015 at 3:39pm
That was my first version. So there's no point to use enum?
Last edited on Apr 30, 2015 at 3:49pm
May 1, 2015 at 8:07am
there isn't if you don't want to reference it.
you have to remember that enum is just a convenient way of defining lots of const ints.
its the same as trying to say
1 2 3
|
const int jaybob = 99;
string myName = jaybob;
|
its not going to work unless you tell the compiler how to convert from numbers to text.
and in your example, even if you did tell the compiler how to do that, how would it cope with this
1 2 3 4
|
enum colours {red=0,white,blue};
enum layers {first=0,second,third};
string test = (string)(rand()%3);
|
what would you expect test to be? a colour or a layer?
Topic archived. No new replies allowed.