Little trouble here using enum in my program I took a section of my program to show maybe one of you have a suggestion in why I am getting an infinite loop here. I have the following statement: enum food {Pizza=10, Lasagna=20, Chips=2, Burrito=5};

case 'c' :
case 'C' :
cout << "What is my favorite food?";
cout << "\nMy top 5 are Pizza,Lasgna,Chips,and Burrito.";
cout << "\nPlease chose from one of my top 5";
cout << endl;
    do{
    cout << "Pick a Food: ";
    cin >> user_selection; 
    if (user_selection < 20)
    cout << "Incorrect please try again";

    else 

    cout << "Good job! You guessed my favorite food!";
    }while (user_selection != 20);
    break;

Edited 3 Years Ago by mike_2000_17: Fixed formatting

When you run the program, and you want to guess that your favorite food is pizza, do actually type in "pizza", or do you input 10?

I type in Pizza.

That won't work. The symbol Pizza is only defined in your program to mean 10. When you type in "Pizza" at the console, it's interpreting it as a char* (I think), and I don't know how that translates to an integral type, but it won't be 10. You need to actually type in the number 10, or interpret the string "Pizza" and realize that it means 10.

1. Where is enum food type values or variables in the snippet? Why "error Using Enum type and loops"?
2. If cin >> user_selection failed (for example, type a letter), you never break loop.

if (cin >> user_selection)
{
   // OK, continue
}
else
{
   // cin is in fail (check cin.fail()) or eof (check cin.eof()) status.
   // if you want to continue:
   cin.clear();
   ...
}

Oh, my God! You typed Pizza...
Input stream errors detection and recovery - see my prev post above...

Actually, the code you wrote should have looked something like (assuming other errors were corrected - code below is not correct!):

if (user_selection < Lasagna)
[INDENT]cout << "Incorrect please try again";[/INDENT]
else 
[INDENT]cout << "Good job! You guessed my favorite food!";[/INDENT]
}while (user_selection != Lasagna);

That's why you use enums in the first place - to make the code more readable (rather than having a bunch of numbers).

However, CoolGamer48 is correct - you need to translate the user's input (which is a string of characters) into an instance of a "food" variable which you could then use in the comparision. The wordy (but effective) way is:

string user_selection;
        food eChoice;
        do{
                cout << "Pick a Food: ";
                cin >> user_selection;
                if (user_selection == "Pizza")
                {
                        eChoice = Pizza;
                }
                else if (user_selection == "Lasagna")
                {
                        eChoice = Lasagna;
                }
                else if (user_selection == "Chips")
                {
                        eChoice = Chips;
                }
                else
                {
// Bad assumption here, but it's an example
                        eChoice = Burrito;
                }
}
                if (eChoice < Lasagna)
                cout << "Incorrect please try again";

                else

                cout << "Good job! You guessed my favorite food!";
                }while (eChoice != Lasagna);

A more elegant approach could be to use a STL Map (type <string, int>) to load up a container of the string values and the associated integer values. Take the user's input, whack it up against the map and get the int value, which you then could use enum tags for the comparison.

Thx for the post guys it helped a lot. Gonna work on it some more pretty sure I will get it now.
thx again.

You can make the code shorter by eliminating the curly brackets for if statements with only one line.

So this:

if (user_selection == "Pizza")
                {
                        eChoice = Pizza;
                }
                else if (user_selection == "Lasagna")
                {
                        eChoice = Lasagna;
                }
                else if (user_selection == "Chips")
                {
                        eChoice = Chips;
                }
                else
                //...

could become:

if (user_selection == "Pizza")
                        eChoice = Pizza;
                else if (user_selection == "Lasagna")
                        eChoice = Lasagna;
                else if (user_selection == "Chips")
                        eChoice = Chips;
                else
                //...

Just to be clear you're assuming user_selection is some kind of string-container (hopefully an std::string), because in the OP is was of type food.

Use std::map? Don't swat a fly with a sledgehammer.

enum Food {Pizza=10, Lasagna=20, Chips=2, Burrito=5};

std::istream& operator >>(std::istream& is, Food& food)
{
	std::string name;
	if (is >> name)
	{
		if (name == "Pizza")
			food = Pizza;
		else if (name == "Lasagna")
			food = Lasagna;
		else if (name == "Chips")
			food = Chips;
		else if (name == "Burrito")
			food = Burrito;
		else
			is.setstate(ios::failbit);
	}
	return is;
}

Now:

Food user_selection;
...
if (cin >> user_selection)
{
  // There is nothing funny in this...
}
else
{
  // add error recovery
  continue;
}
...
while (user_selection != Lasagna);
... // Ooh! Let my people go...
This question has already been answered. Start a new discussion instead.