Enumerator Random

Asked

Viewed 52 times

1

I’m having trouble making an enumerator tube.

enum Notas {A=10, B=22, C=31, D=44, E=56};

There is this solution, but I need the values of the Enumerators and here it is returning the rest of the calculation.

enum Notas {A=10, B=22, C=31, D=44, E=56, ULTIMO};


int teste = static_cast<Notas>(rand() % ULTIMO);

1 answer

1


The problem seems to be that the possible values are not all valid. Instead of directly generating the random number, you will need a conversion.

Generate the number according to the amount of Notes elements. If you have 5 notes, from A to E, generate a number from 0 to 4 using the:

enum Notas {A=10, B=22, C=31, D=44, E=56, ULTIMO};
#define NUMERO_DE_NOTAS 5

int teste = static_cast<Notas>(rand() % NUMERO_DE_NOTAS);

After that, you use the test to enter a switch and with each value return the correct note. If it is 0, returns A, if it is 1, returns B, etc...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.