switch not working

my switch should make jacks, queens and kings values equal 10, rather than 11, 12 and 13, how it seems to be ignored, any ideas?

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
void check151 ()
            {

                for (int j = 0; j < 4; ++j)
                {
                    for (unsigned h = 0; h < hand1.size() - j; ++h)
                    {
                        int a = hand1[j]->getvalue();
                        int b = hand1[j + h]->getvalue();
                        int x = 0;
                        int y = 0;

                        switch (a)
                        {
                            case 11: {x = 10;}
                            case 12: {x = 10;}
                            case 13: {x = 10;}
                            default: {x = a;}
                        }

                        switch (b)
                        {
                            case 11: {y = 10;}
                            case 12: {y = 10;}
                            case 13: {y = 10;}
                            default: {y = b;}
                        }
                        cout << x << " + " << y << " = ";
                        cout << x + y << endl;
                        if (x + y == 15)
                        {
                            tscore1 += 2;
                            cout << "15 score: 2 points" << endl;
                        }
                    }
                }
            }


the cout<< x + y thing was just to see where it was going wrong, which i found out that the switchs were ignored
You need to put in a break statement, else when if the case is 11, it will start there and execute every thing after as well.

Since, cases 11, 12 and 13 all have the same code, you can simplify it:

1
2
3
4
5
6
7
                        switch (a)
                        {
                            case 11: 
                            case 12: 
                            case 13: {x = 10; break;}
                           default: {x = a;}
                        }

Last edited on
of course it was that simple, had an off week, thank you very much
Topic archived. No new replies allowed.