Switch Statement Not Being nice...

So, i am trying to make it so that i can make a character move through an area, and it isnt accessing the cases that should be...


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int mvmnt;
int position = 10050

int movement (int a, int b){
    int x;
    switch(b){
        case 1:
            x = a - 100;
            break;
        case 2:
            x = a + 1;
            break;
        case 3:
            x = a + 100;
            break;
        case 4:
            x = a - 1;
            break;
    }
    return x;
}


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
int main(){
cout << "To move north, press 1. To move East, press 2. To move South, press 3, and to move West, press 4." << endl << endl;
                        cout << "Please make your movement selection. ";
                        cin >> mvmnt;
                        movement(position, mvmnt);
                        switch(position){
                            case 10051:
                                cout << "Movement 1 Works!";
                                cin >> mvmnt;
                                movement(position, mvmnt);
                                break;
                            case 10049:
                                cout << "Movement 2 Works!";
                                cin >> mvmnt;
                                movement(position, mvmnt);
                                break;
                            case 9950:
                                cout << "Movement 3 Works!";
                                cin >> mvmnt;
                                movement(position, mvmnt);
                                break;
                            case 10150:
                                cout << "Movement 4 Works!";
                                cin >> mvmnt;
                                movement(position, mvmnt);
                                break;
                            default:
                                cout << "This area has not been built yet. Please Try again.";
                                break;
                        }


When it runs, and the input is 1, instead of case 9950 being used, the program uses the default case.

any help please?

Thanks
Last edited on
instead of case 9950 being used, the program uses the default case.

That's because position is 10050.
right, but when i input 1, it would call the movement function, and reduce position to 9950... unless i did that wrong...
You are calling the movement function correctly, but you aren't assigning the result it gives to any variable, so it just enters the function then leaves.

One way to fix this would be to change line 5 to
position = movement(position, mvmnt);

Then position will be updated to how it is meant to be.
ah, i see it now... thanks.
Topic archived. No new replies allowed.