Enumerations

Hey guys i'm working on a game using enumerations as my game states, for some reason this does not work,look at my exit statement, for some reason setting it to exit does not stop the loop.

#include <iostream>

using namespace std;



int main()
{

enum GameStates{
GAME,
MAIN_MENU,
PLAYING,
EXIT,
OPTIONS,
};

while (GAME != EXIT)
{
int input;

cout << "Welcome to the GAME!" << endl;
cout << "Choose from the Main Menu" << endl;
cout << "1. Play" << endl;
cout << "2. Options" << endl;
cout << "3. Exit" << endl;
cin >> input;

if (input == 1){

PLAYING;

}
else if (input == 2){

OPTIONS;

}
else if (input == 3){

GAME == 3;

}
else {
system("cls");
cout << "Invalid input" << endl;

}




}






}
Hey. Please use code tags for your code.. It'll be easier to read that way - http://www.cplusplus.com/articles/jEywvCM9/

You're using enums wrong, you're also using C-style enums, I'd recommend c++ enums.

Here is a great video explaining how gameStates work using C++ style enums - https://www.youtube.com/watch?v=Pxvvr5FAWxg
The things you define in an enum are not variables, they're definitions. What you want is a new variable that you set to different values based on those definitions. See currentGameState in the following code:
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
using namespace std;

enum GameStates{
    GAME,
    MAIN_MENU,
    PLAYING,
    EXIT,
    OPTIONS
};

int main()
{
    GameStates currentGameState = MAIN_MENU;

    while (currentGameState != EXIT)
    {
        int input;

        cout << "Welcome to the GAME!" << endl;
        cout << "Choose from the Main Menu" << endl;
        cout << "1. Play" << endl;
        cout << "2. Options" << endl;
        cout << "3. Exit" << endl;
        cin >> input;

        if (input == 1)
        {
            currentGameState = PLAYING;
        }
        else if (input == 2)
        {
            currentGameState = OPTIONS;
        }
        else if (input == 3)
        {
            currentGameState = EXIT;
        }
        else
        {
            system("cls");
            cout << "Invalid input" << endl;
        }
    }

    return 0;
}
Topic archived. No new replies allowed.