Enum with User Input

I need to enumerate blue, red and yellow, then have a user input a number. Depending on what number they input, system output a color.
What am I doing wrong?

1
2
3
4
5
6
7
8
9
10
11
  #include <iostream>
using namespace std;
int number;
int main() {
	enum color {blue=1, red=2, yellow=3};
	cout << "Enter one of the following numbers:" << endl;
	cout << "1 2 3";
	cin >> number;
color usercolor= (color)number;
cout <<  usercolor;
}
Hi,
For your information :
Here is a list of the most used colors in a console window :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
enum myColor
{
        Black = 0, 
        DarkBlue = 1,
        DarkGreen = 2,
        DarkCyan = 3,
        DarkRed = 4,
        DarkMagenta = 5,
        DarkYellow = 6,
        Gray = 7,
        DarkGray = 8,
        Blue = 9,
        Green = 10,
        Cyan = 11,
        Red = 12,
        Magenta = 13,
        Yellow = 14,
        White = 15
};


Here is the function to set color text :
1
2
3
4
5
6
7
8
#include <windows.h>

void consoleTextColor(int k)
{
        HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
   
    SetConsoleTextAttribute(hConsole, k);
}


Here is a list of the colors listed above but in their string form for fast-checking :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
std::string myColorString[] = 
{
        "black", 
        "darkblue",
        "darkgreen",
        "darkcyan",
        "darkred",
        "darkmagenta",
        "darkyellow",
        "gray",
        "darkgray",
        "blue",
        "green",
        "cyan",
        "red",
        "magenta",
        "yellow",
        "white"
};
Does that help you? :)
Not really, I'm not sure how that applies to what this assignment is looking for. Also, I should note that I'm taking intro to computer science, and we just learned about this, so I'm still very hazy on what exactly the purpose of enum is for such short lists (as the assignment calls for).

But thank you for your response!

I think my main problem here is the syntax though, is there anything that is wrong? Because it runs, and it prompts the user, but it returns a number, instead of the color assigned to that number.
> Depending on what number they input, system output a color.
So you are trying to say you want to output the color by name instead of some magical integer value?
Yeah, basically.

From what I understand, enum will assign a number to a list that I input. Then I can reference items in that list by their assigned number, and output them by calling on that number. So, when the user inputs a number when prompted, I want that number to call a color from the list and to be able to output it.

I hope that made sense.
So a couple of steps :
1
2
3
4
5
6
std::string colorname[] = 
{
        "blue", 
        "red",
        "yellow"
};


And the way to output it :
cout << colorname[usercolor] << endl;
Does that help? :)
Yes! It did! Can I ask, why did I have to declare the colors? Is that the only way I could do the cout statement?

Also, thank you so much for being so responsive, it has really helped!

Daniel
> Can I ask, why did I have to declare the colors? Is that the only way I could do the cout statement?
Many. But there is no way better than this :)

> Thank you so much for being so responsive, it has really helped!
Thank you too. Some people are just unresponsive and just take too long to respond, even I often "instantly" give them a reply when their questions just pop up. And more or less, this pretty irritates me.

Glad it helped :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 #include <iostream>
using namespace std;

enum color {
                   BLUE = 1, 
                   RED = 2, 
                   YELLOW = 3};


int main() {

    int userChoice;
    
    cout << "Enter one of the following numbers (1-3): ";
    cin  >> userChoice;
    cout << endl;

    switch(userChoice)
    {
         case BLUE : cout << "Blue";
         break;
         case RED  : cout << "Red";
         break;
         case YELLOW : cout << "Yellow";
         break;
         default : cout << "ERROR - please enter values (1-3)";
         break;
    }

}


1.) user is prompted to enter values 1-3

2.) after value is entered the correct color will be outputted if the wrong value is entered
the user will receive an error.

3.) if you need this to have a loop and be repetitive just let me know.


*ALWAYS put your enum above int main or in header file


This is a more efficient and readable way to write your code
Last edited on
@alleyezonme
Your example is wrong.

1
2
3
4
5
6
case BLUE : cout << "Blue";
break;
case RED : cout << "Blue"; // shouldn't it be "Red" instead???
break;
case YELLOW : cout << "Blue"; // shouldn't it be "Yellow" instead???
break;
I edited it right after i posted I went copy and paste crazy lol :)
closed account (48T7M4Gy)
LOL
Does it just make it more readable to have enum before main, or in a header file, or is there another reason?
Topic archived. No new replies allowed.