0

Noob programmer here, trying to get some help on a assignment. I will use a similar example, if someone can help me figure out what I am doing wrong that would be great. I am aware rand is a integer and it cannot be set equal to enum by default. I am trying to get rand integer to pair a randomly selected enum. Sorry for reposting a question already asked, but other examples written by other users kinda confuse me.

NOTE: I did not add srand to seed random number because in my specific assignment, it does not need to be seeded according to my instructor, not sure why but just following instructions.

#include <iostream>
#include <ctime>
#include <iomanip>

using namespace std;

int main(){ 

  enum SHIRT_COLOR { WHITE, BLACK, RED, GREEN};
  int value = 0; //rand num to be generated
  value = rand() % 4;

  SHIRT_COLOR shirt = WHITE;
  SHIRT_COLOR shirt = BLACK;
  SHIRT_COLOR shirt = RED;
  SHIRT_COLOR shirt = GREEN;
  shirt = static_cast<SHIRT_COLOR>(value);

  cout << "Random Shirt Color: " << shirt;

}    
  • 1
    It seems like you're required to use `rand()`? You're not allowed to use the superior funcitonality of the `` library? – JohnFilleau Mar 09 '20 at 01:55
  • Oh I see you want to output like "WHITE", "BLACK", etc? – JohnFilleau Mar 09 '20 at 01:57
  • @John have not gone over the library in my CS class yet – Haik Hakopyan Mar 09 '20 at 01:58
  • @John correct, instead of outputting the random number, I want that random number to be matched with one of the colors set on enum – Haik Hakopyan Mar 09 '20 at 01:59
  • 3
    How do you expect to derive only four possible, different, `enum` values from a random number that can have five possible, different values? As Mr. Spock would say: this is highly illogical. – Sam Varshavchik Mar 09 '20 at 01:59
  • What is your question? What is the problem? – Asteroids With Wings Mar 09 '20 at 02:00
  • By the way, fair enough about the seed if you were instructed not to use it, as long as you personally remain aware that the instructor is wrong. – Asteroids With Wings Mar 09 '20 at 02:00
  • If I set rand to % 5 couldnt that restrict it to only 0-4? Then I have 4 colors to choose from, to match to the random number. 0 = White 1=Black ,, etc – Haik Hakopyan Mar 09 '20 at 02:01
  • Check out https://stackoverflow.com/questions/28828957/enum-to-string-in-modern-c11-c14-c17-and-future-c20. I like the approach of creating a `const map`, but a `switch`/`case` statement is probably more aligned to where you are in class. – JohnFilleau Mar 09 '20 at 02:01
  • _"Sorry for reposting a question already asked, but other examples written by other users kinda confuse me."_ If it's been asked before, and you didn't understand the answers, perhaps you should tell us what you didn't understand about them so that we don't just waste our time re-explaining in a way that you once again won't understand! – Asteroids With Wings Mar 09 '20 at 02:02
  • EDIT: I edited rand() to show % 4 not 5 sorry for the mistake – Haik Hakopyan Mar 09 '20 at 02:02
  • You have to write the code to translate each possible enum value to a literal text string that contains its name. You will have to do all this work yourself. C++ will not do it for you. There's very little that C++ does automatically, you always need to do all the work yourself. – Sam Varshavchik Mar 09 '20 at 02:06
  • @John Okay, I will check out that page thank you. – Haik Hakopyan Mar 09 '20 at 02:06
  • @Sam I understand I need to do it myself, just dont know how to convert a int value to a enum value. I know my example is wrong, but thats what I was able to put together, figured it was not correct since I got errors in compiler. – Haik Hakopyan Mar 09 '20 at 02:07
  • You can convert an int value to an enum using a `static_cast`. But that won't get you the enum value's name, as a literal string. – Sam Varshavchik Mar 09 '20 at 02:08
  • There is a wide gaping chasm between having a standard library in a programming language, and having an AI-controller matter replicator. I concede there is no reflection in C++, but to claim that "you always need to do all the work yourself" is just comically wrong, and you well know it. There are _tons_ of useful tools in the standard library. Go back and write some C if you've forgotten how lucky you are! – Asteroids With Wings Mar 09 '20 at 02:10
  • @Sam okay so that gets me closer to understanding it, so I am suppose to conver the int value using static_cast, but them maybe convert that into string? so instead of my output showing 3, it will show Green? – Haik Hakopyan Mar 09 '20 at 02:11
  • Yes, you will have to do that, and, contrary to others' advice, you will have to write the code to do it yourself. C++ will not do it for you, automatically. Or, you can try to ask them where's the button on your keyboard that makes the code to do this pop out into existence, when pressed. Maybe they know something I don't. – Sam Varshavchik Mar 09 '20 at 02:14
  • @Sam I never came here with the intentions of having it done for me. Better yet, I did not pick Computer Science as my degree because I assumed the code will be done for me automatically. So if that is what I made it seem like, my apologies. – Haik Hakopyan Mar 09 '20 at 02:16

1 Answers1

2

There are two problems here, but neither of them have anything to do with rand().


Multiple declarations

  SHIRT_COLOR shirt = WHITE;
  SHIRT_COLOR shirt = BLACK;
  SHIRT_COLOR shirt = RED;
  SHIRT_COLOR shirt = GREEN;

Here you create four variables with the same name. It's not clear why you do this.

Just make one variable for the shirt colour you've chosen:

  SHIRT_COLOR shirt = static_cast<SHIRT_COLOR>(value);

Output

C++ doesn't know how to print your enums, out of the box, and your program doesn't know about these variable/constant names; they only exist in your source code.

Presumably, then, you are receiving a compilation error on this line:

cout << "Random Shirt Color: " << shirt;

You'll need to sort this yourself, either with a std::map, or a switch, or even just a series of if/else if given the scale involved.


Other than that, your approach is fine.

The static_cast makes sense.

Regarding srand(), fair enough if you have to omit it to pass your assignment, but your instructor is wrong to claim that it should not be in there.

Furthermore, in production code in the future you shall be using the C++11 random features, not ancient rand(). You'll probably never be taught that in school, because schools do not teach production-quality C++, but it is something you should look into yourself either now or when you hit the jobs market.

Asteroids With Wings
  • 17,071
  • 2
  • 21
  • 35