Default not working in switch statement!!

Pages: 123
Okay…now I’m getting frustrated. I’m trying to fix a problem pointed out to me in my switch. Originally, I was not using a loop in the default clause and after entering an invalid response twice, it drops out and goes on to the next line of code. I thought that by creating a loop it would force the user to put in the correct value. That part works but when the correct value is entered it still doesn’t drop out of the loop. Nor does it go back to the switch. I’m at a complete loss right now. I’ve tried the plain jane default and that doesn’t work; I’ve tried a while loop (the current one), a do while loop, and even tried doing it as an if/else statement. What’s wrong with this 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
void choice1 (int &pStrength, int &bStrength, double &milkMoney)
{
     char choice   = ' ';
     
     cout <<"How do you wish to get to school today?" << endl;
     cout <<"Please press: " << endl;
     cout <<"   S to take the school bus" << endl <<"   B to ride your bike" << endl;
     cout <<"   W to walk to school" << endl;
     cin  >> choice;
     choice = toupper(choice);
     
     switch (choice)
     {
            case 'S':
                 {
                     outcomes (pStrength, bStrength, milkMoney);
                     break;
                 }
            case 'W':
                 {
                     outcomes (pStrength, bStrength, milkMoney);
                     break;
                 }
            case 'B':
                 {
                     outcomes (pStrength, bStrength, milkMoney);
                     break;
                 }
            default:
                 {
                     while (choice != 'S' || choice != 'W' || choice != 'B')
                     {
                           cout <<" You've entered an invalid response. Please only select 'S', 'B', or 'W'." << endl;
                           cin >> choice;
                     }
                     break;
                 }
     }
}
Awww... cases the way they should be. <3

But seriously, I don't think the brackets are a good idea.

-Albatross


Extension: If you want to force the user to input the correct value, I'd recommend an infinite while loop. A bool variable might also come in handy for breaking that loop.
Last edited on
Albatross,

Can you give an example of how I may go by doing either one of your suggestions? My brain is fried. I've been looking at this one problem for hours now. Thanks in advance.
1
2
3
4
5
6
7
 default:

                     while (choice != 'S' && choice != 'W' && choice != 'B')
                     {
                           cout <<" You've entered an invalid response. Please only select 'S', 'B', or 'W'.\n" 
                           cin >> choice;
                     }

Try removing break under your default case.
Last edited on
Unfortunately, it still doesn't drop out of the loop.
i tryed it like this worked for me
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
 #include <iostream>
 using namespace std;



int main(){
 char choice   = ' ';

     cout <<"How do you wish to get to school today?" << endl;
     cout <<"Please press: " << endl;
     cout <<"   S to take the school bus" << endl <<"   B to ride your bike" << endl;
     cout <<"   W to walk to school" << endl;
     cin  >> choice;
     choice = toupper(choice);

     switch (choice)
     {
            case 'S':
                 {

                     break;
                 }
            case 'W':
                 {

                     break;
                 }
            case 'B':
                 {

                     break;
                 }
            default:

                     while (choice != 'S' && choice != 'W' && choice != 'B')
                     {
                           cout <<" You've entered an invalid response. Please only select 'S', 'B', or 'W'." << endl;
                           cin >> choice;
                     }


     }
     return 0;
}
Last edited on
I think you meant to have &&, not ||. If you have ||, then even if choice == 'S', it will be != 'W' and the loop will continue.
lol yea
firedraco,
but using the && statement means that both outcomes have to be true. Using || allows for any of the option to be true in order to drop out of the loop.

CodeSmoker,
That's interesting. you can run it and it works fine but I can't. Do you think it could be a glitch in my compiler? I'm using DevC++ and already, I've found at least two glitches already.
firedraco,
I did at least give it a try after noticing that CodeSmoker had used the && in his working version. It still doesn't drop out of the loop though. See guys, this is why this damned thing is frustrating me. Everything I try, nothing works. I either get stuck in the loop or it falls out without going back to the switch. Do you think it would would helpful if you had the whole code? Its not a long program.
Using codeblocks.
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#include <iostream>
#include <algorithm>
#include <time.h>
using namespace std;


void dailyRoutine (int &num);
void choice1 (int &pStrength, int &bStrength, double &milkMoney);
void outcomes (int &pStrength, int &bStrength, double &milkMoney);
void fight (int &pStrength, int &bStrength, double &milkMoney);
void walk (int &pStrength, int &bStrength, double &milkMoney);
void days (int &num);
void dowStart (int &num);

int main ()
{
    int num   = 0;
    
    cout <<"To start, please press 1." << endl;
    cin  >>num;
    dowStart (num);
    
    
    dailyRoutine (num);
    
    system ("pause");
    return 0;
    
}

void dailyRoutine (int &num)
{
     int pStrength     =  100;
     int bStrength     =  101;
     double milkMoney  = 1.25;
     
     for (num =1; milkMoney >= .25; num ++)
     {
         days (num);
         cout <<"Your current strength is " << pStrength <<"." << endl;
         cout <<"The bully's current strength is "<< bStrength <<"."<< endl;
         cout <<"You have "<< milkMoney <<" left to spend on milk." << endl << endl;
         choice1 (pStrength, bStrength, milkMoney);
     }
     
     if (pStrength > bStrength)
    {
         cout <<"With Final Strength of " << pStrength <<" our winner is..." << endl << endl << endl;
         cout <<"                       YOU!!!" << endl << endl;
         cout <<"CONGRATULATIONS!!! YOU DEFEATED THE BULLY ONCE AND FOR ALL!" << endl;
    }
    else if (bStrength > pStrength)
    {
         cout <<"With Final Strength of " << bStrength <<" our winner is..." << endl << endl << endl;
         cout <<"                       THE BULLY" << endl << endl;
         cout <<" Tough break kid. You tried, I grant you that. Maybe next week." << endl;
    }
    else if (pStrength == bStrength)
    {
         cout <<"With a matching strength of " << pStrength <<" our winner is..." << endl << endl << endl;
         cout <<"                        NO ONE!!!" << endl << endl;
         cout <<" Because you and the bully were tied at the end of the game, we have no winner." << endl;
         cout <<" Please play the game again to try again at beating the bully." << endl;
    }
}

void choice1 (int &pStrength, int &bStrength, double &milkMoney)
{
     char choice   = ' ';
     
     cout <<"How do you wish to get to school today?" << endl;
     cout <<"Please press: " << endl;
     cout <<"   S to take the school bus" << endl <<"   B to ride your bike" << endl;
     cout <<"   W to walk to school" << endl;
     cin  >> choice;
     choice = toupper(choice);
     
     switch (choice)
     {
            case 'S':
                 {
                     outcomes (pStrength, bStrength, milkMoney);
                     break;
                 }
            case 'W':
                 {
                     outcomes (pStrength, bStrength, milkMoney);
                     break;
                 }
            case 'B':
                 {
                     outcomes (pStrength, bStrength, milkMoney);
                     break;
                 }
            default:
                 {
                     while (choice != 'S' && choice != 'W' && choice != 'B')
                     {
                           cout <<" You've entered an invalid response. Please only select 'S', 'B', or 'W'." << endl;
                           cin >> choice;
                     }
                     
                 }
     }
}

void outcomes (int &pStrength, int &bStrength, double &milkMoney)
{
     srand (time(0));
     int randNum    = 0;

     randNum = 1 + rand() % (2 - 1 + 1);

     if (randNum == 1)
     {    
          cout << "You did not run into the bully today." << endl;
          cout << "You bought milk today for 25 cents." << endl;
          cout <<"Because you had your milk for the day, your" << endl;
          cout << "strength has increased by 1 point." << endl << endl << endl;
          pStrength = pStrength + 1;
          milkMoney = milkMoney - .25;
     }
     else if (randNum == 2)
     {
          walk (pStrength, bStrength, milkMoney);
          cout << endl << endl << endl;
     }
}

void walk (int &pStrength, int &bStrength, double &milkMoney)
{
     char choice2  = ' ';
     
     cout << endl << "Good Grief! You ran into the bully today. What would you like to do?" << endl;
     cout <<"Please press: " << endl;
     cout <<"   F to fight and stand up for yourself" << endl <<"   R to give up and run away like a little girl" << endl;
     cin >> choice2;
     choice2 = toupper(choice2);
                
              
                switch (choice2)
                { 
                     case 'F':
                     {
                          fight(pStrength, bStrength, milkMoney);
                          break;
                     } 
                     case 'R':
                     {
                          cout << "You give the bully your 25 cents. He lets you go but you don't get any milk today." << endl;
                          cout << "He takes your 25 cents and drinks your milk, adding 1 point to his strength." << endl << endl;
                          bStrength = bStrength + 1;
                          milkMoney = milkMoney - .25;
                          break;
                     } 
                     default:
                     {
                          cout << "Invalid response. Please enter only 'F' or 'R'." <<endl;
                          cin >> choice2;
                          break;
                     } 
                } 
}

void fight (int &pStrength, int &bStrength, double &milkMoney) 
{
  
    srand (time(0));
    int pPunch       =   0;
    int bPunch       =   0;
    
  
    pPunch = 1 + rand() % (pStrength - 1 + 1);
    bPunch = 1 + rand() % (bStrength - 1 + 1);
    
    cout <<"You hit the bully with a punch strength of " << pPunch <<"." << endl;
    cout <<"The bully hit you with a punch strength of " << bPunch <<"." << endl << endl;

 
    if (pPunch > bPunch)
    {
               cout << "Yea! You defeated the bully." << endl;
               cout << "You bought milk today for 25 cents. Because you had your milk for the day," << endl;
               cout << " your strength has increased by 1 point." << endl; 
               pStrength = pStrength + 1;
               bStrength = bStrength - 1;
               milkMoney = milkMoney - .25;
    }
    else if (pPunch < bPunch)
    {
               cout << "Awww... Poor thing. The bully has defeated you." <<endl <<"And worse yet, he took your 25 cents" << endl << "and drank your milk." << endl;
               cout << "He adds 1 point to his strength while you take the day to recover." << endl <<"You lose 1 point." << endl;
               pStrength = pStrength - 1;
               bStrength = bStrength + 1;
               milkMoney = milkMoney - .25;
    }
    else if (pPunch == bPunch)
    {
               cout <<"You and the bully fight to a tie." << endl;
               cout <<"You lose your 25 cents during the fight." << endl;
               cout <<"Neither of you change strength." << endl << endl;
               milkMoney = milkMoney - .25;
    }
    
}


void days (int &num)
{
     if (num == 1)
     {
          cout << "Today is Monday." << endl;
     }
     else if (num == 2)
     {
          cout << "Today is Tuesday." << endl;
     }
     else if (num ==3)
     {
          cout << "Today is Wednesday." << endl;
     }
     else if (num ==4)
     {
          cout << "Today is Thrusday." << endl;
     }
     else if (num ==5)
     {
          cout << "Today is Friday." << endl;
     }
}

void dowStart (int &num)
{
     if (num == 1)
     {
          cout << "Today is Monday." << endl;
     }
     else  
     {
           while (num != 1)
           {
                 cout << endl << "I said press 1 to start. When you make your game you may have the " << endl <<"player press " << num <<" to start your game. This is my game." << endl;
                 cout << endl << "Please prese 1 to begin." << endl;
                 cin >> num;
                 cout << endl;
           }
     }
}


So this is a simple little game but the default doesn't work.
You do know that in the first part of the code, you used toupper(choice) to change to upper case. So uou could enter b and get B

In the loop you don't use toupper - so to get out of the loop you will have to use the CAPS or SHIFT key to get the captital letter B, W or S.
Good catch...I like that....now, how do I get it to go back through the switch or should I put the switch in the while loop?
Okay, that seemed to have worked. I added the toupper function and then copied the switch into the loop and it worked. I'm sure there has to be another way of doing this that is shorter but this works for the scope of the class. Thanks guys...
This is what worked:

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
52
53
54
55
56
57
58
void choice1 (int &pStrength, int &bStrength, double &milkMoney)
{
     char choice   = ' ';
     
     cout <<"How do you wish to get to school today?" << endl;
     cout <<"Please press: " << endl;
     cout <<"   S to take the school bus" << endl <<"   B to ride your bike" << endl;
     cout <<"   W to walk to school" << endl;
     cin  >> choice;
     choice = toupper(choice);
     
     switch (choice)
     {
            case 'S':
                 {
                     outcomes (pStrength, bStrength, milkMoney);
                     break;
                 }
            case 'W':
                 {
                     outcomes (pStrength, bStrength, milkMoney);
                     break;
                 }
            case 'B':
                 {
                     outcomes (pStrength, bStrength, milkMoney);
                     break;
                 }
            default:
                 {
                     while (choice != 'S' && choice != 'W' && choice != 'B')
                     {
                           cout <<" You've entered an invalid response. Please only select 'S', 'B', or 'W'." << endl;
                           cin >> choice;
                           choice = toupper(choice);
                           switch (choice)
                           {
                                  case 'S':
                                  {
                                       outcomes (pStrength, bStrength, milkMoney);
                                       break;
                                  }
                                  case 'W':
                                  {
                                       outcomes (pStrength, bStrength, milkMoney);
                                       break;
                                  }
                                  case 'B':
                                  {
                                       outcomes (pStrength, bStrength, milkMoney);
                                       break;
                                  }
                           }
                     
                     }
                 }
     }
}


Thank you all for the help.
Could try this:
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
void choice1 (int &pStrength, int &bStrength, double &milkMoney)
{
     char choice   = ' ';
     
     cout <<"How do you wish to get to school today?" << endl;
     cout <<"Please press: " << endl;
     cout <<"   S to take the school bus" << endl <<"   B to ride your bike" << endl;
     cout <<"   W to walk to school" << endl;
     cin  >> choice;
     choice = toupper(choice);

     do{

     switch (choice)
     {
            case 'S':
                 {
                     outcomes (pStrength, bStrength, milkMoney);
                     return;
                 }
            case 'W':
                 {
                     outcomes (pStrength, bStrength, milkMoney);
                     return;
                 }
            case 'B':
                 {
                     outcomes (pStrength, bStrength, milkMoney);
                     return;
                 }
            default:
                 {
                     while (choice != 'S' && choice != 'W' && choice != 'B')
                     {
                           cout <<" You've entered an invalid response. Please only select 'S', 'B', or 'W'." << endl;
                           cin >> choice;
                           choice = toupper(choice);
                     }
                     
                 }
     }
}while(true);
}


Should make the switch loop until you enter one of the cases.
True. I'll give that a try.

Can someone post an example of a function that returns a bool? I never really understood how bool works. I know it can only return true or false but how and when to use them...I'm at a loss.
I tried the do/while loop and it although that does work (so to speak) it creates a cool looking infinite loop.
how does it make an infinite loop? did you change break; to return; in the cases?
Pages: 123