Enumerations, an if statements

closed account (LN7oGNh0)
I was experimenting and unfortunately it didn't work. I tried using enumerations and if statements with else clauses. I only want to use these things because I want to expand my knowledge of enumerators because I don't really understand them. There is an explanation of what I wanted the code to do if you don't understand my code.


#include <iostream>

using namespace std;
using std::endl;

int main()
{
cout << "Difficulty levels";
cout << "\n\n1-Easy";
cout << "\n\n2-Normal";
cout << "\n\n3-Hard";

int choice;
cout << "\n\nChoice:";
cin >> choice;

enum choice {EASY, MEDIUM, HARD};

if (choice = EASY)
{
cout << "You have chosen EASY!";
}

else if (choice = MEDIUM)
{
cout << "You have chosen MEDIUM!";
}

else if (choice = HARD)
{
cout << "You have chosen HARD!";
}

else
{
cout << "Cant choose that.";
}

system("\n\n\npause");
return 0;
}




That is my code. What I wanted it to do was to make the player of some video game choose what difficulty they wanted. With the difficulties, I did not know whether the player should have typed i a number to choose their difficulty or the actual name of the difficulty. (Second one sounded like the one I should have done...)

Thank you.
enums start at zero, not one. However, you can assign a value as in:
 
enum choice { easy = 1, medium, hard };
Last edited on
= is the assignment operator, comparison is done with ==.
closed account (LN7oGNh0)
That didnt fix the problem. I know i should have done that, but i'm suspecting that there is more I need to do.
closed account (LN7oGNh0)
Thank you Athar, that fixed the problem. I have one question though. How come i had to use two equal signs? I usually use 1 and my program woulr run fine...
I doubt they ran fine. While this compiles, it certainly doesn't do what you want.
If you write
if (choice = EASY)
it has the same effect as:
1
2
choice = EASY;
if (choice)


When an integral value like EASY is converted to a bool, 0 will be converted to false and everything else to true.
closed account (LN7oGNh0)
Ya, I had to make a couple changes to the code but it did what I wanted in the end. Thank you.
Topic archived. No new replies allowed.