1

i have below enum :

enum words= {dog, cat, horse, hen, goat, pig, sheep};

can i assign a new enum constant as below :

words word = words[4];

What i want to do is get a random number between 0 and words.size() .and i want to pick that word from the enum . is it possible?

int wordno = (rand()%8);

words word = (words)wordno;
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
  • `words word = (words)wordno;` would work. Why do you need operator `[]` while cast could achieve it? – Louis Go Jan 11 '22 at 05:05
  • Shivani Garg, With 7 in `{dog, cat, horse, hen, goat, pig, sheep};`, `int wordno = rand()%7;` makes more sense. – chux - Reinstate Monica Jan 11 '22 at 05:11
  • If you add an additional element `num_words` to the `enum`, then this element will have the value of the number of elements in the array. You can then use `num_words` instead of hard-coding the value `7` or `8`. – Andreas Wenzel Jan 11 '22 at 05:32

1 Answers1

3

Your enum declaration is a bit wrong. It should be

enum words {dog, cat, horse, hen, goat, pig, sheep};

Since C++11 it is usually also better to use an enum class instead, see this question for details:

enum class words {dog, cat, horse, hen, goat, pig, sheep};

Note that enum enumerators already have a numeric value. If you don't specify these numeric values manually, they are from left-to-right starting from zero increasing by one. So e.g. horse has the numeric value 2 in your enumeration.

However, it is not directly allowed to convert between the numeric value and the enumeration implicitly. If you need to do that, you must use static_cast:

auto word = static_cast<words>(wordno);

When doing this, you need to be careful however (which is also why the explicit cast is required), because you are not allowed to cast an integer value that is outside the range of numeric values of the enumeration in this way. In your code you do rand()%8 which can return values up to 7, but the largest numeric value in your enumeration is 6 for sheep. You would have undefined behavior with that.

auto can be replaced by words. It just lets the compiler figure out the correct type, so I don't have to write it twice. (It is already in the static_cast.)

As a sidenote, rand is not a good random number generator for many reasons (see e.g. this question). Since C++11 you can use the more modern <random> library, see this question.

Also as a sidenote I suggest avoiding C-style casts such as (words)wordno. Instead use static_cast as I showed above. You are much less likely to make serious mistakes this way, since the C-style casts are usually too permissive in what casts they allow.


For words word = words[4];, this syntax does not work. But it is not really necessary, since as above, you can initialize word directly with the value 4 with an explicit cast.

auto word = static_cast<words>(4);

But if you are intending to assign a constant value, you should just use the enumerator directly. That is what they are for:

auto word = words::goat;
user17732522
  • 53,019
  • 2
  • 56
  • 105
  • i want the enum to take a different constant value (from the given values in {}) in every run. but in this case it just takes the value to be the random number that is generated. Instead i want it to hold the values as 'dog' or'cat' – Shivani Garg Jan 12 '22 at 15:38
  • @ShivaniGarg It is not clear to me what you mean. After the assignment, `word` will be of type `words` and hold one of `words::dog`, `words::cat`, etc. randomly. – user17732522 Jan 12 '22 at 15:43
  • int main(){ enum words { dog, cat, horse, hen, goat, pig, sheep} srand(time(0)); int wordno = (rand()%7); //words word = (words)wordno; words word = static_cast(wordno); std::cout< – Shivani Garg Jan 12 '22 at 15:54
  • @ShivaniGarg The name of an enumerator is a purely compile-time concept. You cannot get a string to print from an enumerator's name, the same as you can't get a string to print from a variable's name. If you want to assign strings to the enumerators, write a function that uses `switch` on `words` and returns the corresponding string to each enumerator. – user17732522 Jan 12 '22 at 15:57