why doesn't this work...

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
48
49
50
51
#include <iostream>

using namespace std;

enum DIRECTION_t
{
    int NO_MOVEMENT;
    int LEFT;
    int RIGHT;
    int UP;
    int DOWN;
};

int main()
{
    int Choice = NO_MOVEMENT;

    cout << "What direction do you want to move? Enter 1, 2, 3, or 4" << endl;
    cin >> Choice

    switch(Choice)
    {
        case LEFT:

        cout << "You moved left" << endl;
        break;

        case RIGHT:

        cout << "You moved right" << endl;
        break;

        case UP:

        cout << "You moved up" << endl;
        break;

        case DOWN:

        cout << "You moved down" << endl;
        break;

        default NO_MOVEMENT:

        cout << "You moved nowhere" << endl;
        break;
    }

    return 0;
}


I clearly don't know anything about using enums.
closed account (z05DSL3A)
The enum should be
1
2
3
4
5
6
7
8
enum DIRECTION_t
{
    NO_MOVEMENT,
    LEFT,
    RIGHT,
    UP,
    DOWN
};


you are missing a ; on line 19

Line 43 should be default:
Not only that, Grey Wolf, but he doesn't use his enum anywhere in his program. Choice is of type INT, so it can't store "DOWN". It can store the ASCII VALUE of DOWN, but not text. If you want to store text in a variable, the variable needs to be of type string, or a char array. Also, to declare a variable of your enum type, you need to do this...

1
2
3
4
5
6
7
8
9
#include <iostream>

enum DIRECTION_t {NO_MOVEMENT, LEFT, RIGHT, UP, DOWN};

int main(int argc, char* argv[])
{
DIRECTION_t direction;
return 0;
}


Also, when declaring an enum, you do not give the enum members a data type like int.

It sounds like you've never seen a data type besides int. Perhaps go back and study simple and structured data types before trying this again.
closed account (z05DSL3A)
It sounds like you've never seen a data type besides int. Perhaps go back and study simple and structured data types before trying this again.

Who is that aimed at?
@GreyWolf That was intended for shadowvillain. Sorry if there was some confusion.
closed account (z05DSL3A)
Still, it was very rude; especially as enum in an integer type and if the changes are made as I outline the program would work.
How was it rude? I was making an observation as he is not using enum correctly.
closed account (z05DSL3A)
No, the bit about "It sounds like you've never seen a data type besides int..."; but whatever.
Because the way he's coding, he doesn't seem to know about other data types. If he did, he would know that an integer cannot hold anything but integers. It can store the ASCII value of text, as I said, but for what he's looking to do, it will not work.
I didn't know you could use any other data type for enums my book I'm learning from has like a half of page about enums. I hadnt even gotten to the easy errors because the enum errors where higher up so I addressed those first.
Last edited on
I didn't know you could use any other data type for enums my book I'm learning from has like a half of page about enums.

And you can't. An enum is always based on a set of integers. But notice that enums are types of their own.
@shadowvillain
That's not what I was getting at. I meant that you declared your enum incorrectly and didn't even use it in your program.
Oh ok.
Topic archived. No new replies allowed.