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>
usingnamespace 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;
}
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?
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.
> 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.
#include <iostream>
usingnamespace 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
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;