Why won't this switch statement work

Sep 24, 2021 at 12:32am
So I've just started making a text based snakes and ladders game in C++ but this switch statement just keeps on returning the default case.



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
#include <iostream>


using namespace std;


int main(){


    int player1pos = 0;
    int player2pos = 0;

    int playerturn = 1;

    int dice = 1;

//   int input;
//   cin >> input;

   const int lad1 = 9;     const int lad1length = 37;
   const int lad2 = 23;    const int lad2length = 11;
   const int lad3 = 44;    const int lad3length = 18;
   const int lad4 = 67;    const int lad4length = 23;
   const int lad5 = 85;    const int lad5length = 9;

    switch (player1pos) {
  case lad1:
        player1pos += lad1length;

        cout << player1pos << endl;
        break;

    default:
        cout << "not working" << endl;
   }

    return 0;
}
Sep 24, 2021 at 1:09am
What were you expecting?

Line 10: You initialize player1pos to zero and do not change it.

Line 27 expects player1pos to be 9.
Any other value (including zero) will execute the default case.

Sep 24, 2021 at 1:28am
Holy crap I feel so stupid I was stuck with this for like an hour and a half

Thanks anyway :)
Topic archived. No new replies allowed.