swich with if statement not working

Hey all, I have a switch that has an if statement in one of the cases. This if statement, if true, is supposed to switch the case... but it doesn't. It will print the cout statement but it skips over my "state=RESET" initialization and goes right to the cout statement after my if statement. Its located on lines 136 and 141. What am I doing wrong?

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
 #include "stdafx.h"
#include <iostream>
#include <cstdlib>        // needed for rand() and srand()
#include <ctime>          // needed for time()
using std::cout; using std::endl;
using std::cin;

int deck[52]={2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10,10,10,10,10,10,10,10,10,
		10,10,10,10,11,11,11,11};
	
int getCardCalled=-1;
int getcard();

int main()						//Suffle function
{
	int dtotal=0, ptotal=0, dsubtotal=0;
	int i,p,temp;						// variable used as array INDEX, to select which array element.
	int pcard1, pcard2=0, dcard1, dcard2, pnewcard,dnewcard, pdeal=2,ddeal=2;
	char go;
	enum {INITIAL, HIT, STAND, BUST, RESET} state;
  
  
  srand(time(NULL));			// seed for random function
								// set each of the 52 cards to a random value
  
		cout<<endl;cout<<endl;

	for (p=0; p<520; p++){
		int k = rand()%52;
		int l = rand()%52;
			temp=deck[k];
			deck[k]=deck[l];
			deck[l]=temp;
	}	
  
	for (i=0; i<52; i++){ 
		cout<<deck[i]<<" "; 
	}

		cout<<endl;cout<<endl;

	cout<<"Welcome to Blackjack"<<endl;
		cout<<endl;cout<<endl;

	cout<<"Are you ready to play? Enter y for yes and n for no"<<endl;
	cin>>go;

	if (go=='n' || go=='N'){ 
 
              cout<<"Bye,Bye!"<<endl;
              exit(0);              // end the program 
	}

	 if (go=='y' || go=='Y'){
	state = INITIAL;
	}
		cout<<endl;cout<<endl;cout<<endl;
	

  while (1){
	
	switch (state){
		
	case INITIAL:
	
		cout<<"Dealer will now Shuffle the cards and deal."<<endl;	
		
			pcard1=getcard(); 
			pcard2=getcard();
			dcard1=getcard();
			dcard2=getcard();
		
		if (pcard1==11 && pcard2==11){
			cout<<"You have drawn two aces.  One has been changed to a 1 in order to avoid busting"<<endl;
			
				pcard1=1;
		} //end if
		
			ptotal=pcard1+pcard2;
		
		cout<<"Your cards are:  "<<pcard1<<"   "<<pcard2<<endl;
		cout<<"Your total is:   "<<ptotal<<endl;	
		
		
		if (dcard1==11 && dcard2==11){
			
			if(dcard1==11)
				dcard1=1;
		}  //end if
			
		cout<<"The dealer has "<<dcard1<<"   "<<"X     "<<endl;
		
		
		cout<<"Would you like to Hit or Stand? H or S "<<endl;
		cin>>go;
		
		if (go == 'H'|| go == 'h'){
				state = HIT;
		}
		if (go == 's'|| go == 'S'){
				state = STAND;
		}
	break;
	
	case HIT:
			pnewcard = getcard();
	
			pdeal=pdeal+1;
	
		cout<<pdeal<<endl;
		
		if (ptotal+pnewcard>21 && pcard2==11 ){
				pcard2=1;
				ptotal=ptotal-10;
		
			cout<<"The value of your ace has been changed from 11 to 1 in order to avoid busting"<<endl;
		}
	
	
		if(pnewcard == 11 && ptotal+pnewcard>21){
				pnewcard=1;
	
			cout<<"You were delt an ace. The value of ace has been changed from 11 to 1 in order to avoid busting"<<endl;		
		}
	
	
	
		cout<<"Your new card is :   "<<pnewcard<<endl; 
	
			ptotal=ptotal+pnewcard;
		
		cout<<"Your new total is:   "<<ptotal<<endl;
	
		if (ptotal>21){
			cout<<"You have Busted!!"<<endl;
				state =  RESET;
		}  //end if
	
		if(pdeal>11){
			cout<<"You have Busted!!"<<endl;
				state =  RESET;
		}  //end if
		
		cout<<"Would you like to Hit or Stand?"<<endl;
		cin>>go;
	
		if (go == 'H'|| go == 'h'){
				state = HIT;
		}
		
		if (go == 's'|| go == 'S'){
				state = STAND;
		}


	break;

	case STAND:
		cout<<"The dealer is drawing cards"<<endl;
	
			dtotal=dtotal+dcard1+dcard2;

		while (dtotal<=17){
	
				dnewcard = getcard();
				dtotal=dtotal+dnewcard;
		}
	
		cout<<"Your total card value is "<<ptotal<<endl;
		cout<<"Dealer total card value is  "<<dtotal<<endl;	
	
		if (dtotal>21){
			cout<<"Dealer has busted. YOU WIN!!"<<endl;
		}
	
		
		if (ptotal>dtotal){
	
			cout<<"YOU HAVE WON"<<endl;
		}	

	
			state =  RESET;


	case RESET:
	
		cout<<"Would you like to play again? Type y for yes or n for no to exit."<<endl;
		cin>>go;
	
		if (go=='n' || go=='N') {
 
            cout<<"Bye,Bye!"<<endl;
				exit(0);
		}

		if (go=='y' || go=='Y'){
		
				pdeal=2;
				ddeal=2;
		
			for (p=0; p<520; p++){
  
					int k = rand()%52;
					int l = rand()%52;
					temp=deck[k];
					deck[k]=deck[l];
					deck[l]=temp;
			}
  
			for (i=0; i<52; i++){
  
				cout<<deck[i]<<" "; 
			}  

			cout<<endl;cout<<endl;
		
			getCardCalled=-1;
			state = INITIAL;

		} //end if
	
	break;	

	default: cout<<"default"<<endl;
	
	break;	
		
		}	//end switch
	}   //end while
}	//end main

       //deal function
int getcard() {
	++getCardCalled;
	return deck[getCardCalled];
}




This is because you forgot the break statement. Also, if you have multi-line case statements it's good to add curly-braces around the block. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
case 1: {
          baz = foo;
          foo = (char)foobar;
          barfoo = foobar - foo;
          foo++;
          break;
        }
case 2: {
          ...;
          ...;
          ...;
          break;
        }
....;
Last edited on
Brilliant!! Thanks! I knew it had to be something simple. Thanks for the advice. I will ad those curly-braces.
Doesn't it seem strange to you to have a two line function

1
2
3
4
int getcard() {
	++getCardCalled;
	return deck[getCardCalled];
}


and a fifty line switch condition?

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
case HIT:
			pnewcard = getcard();
	
			pdeal=pdeal+1;
	
		cout<<pdeal<<endl;
		
		if (ptotal+pnewcard>21 && pcard2==11 ){
				pcard2=1;
				ptotal=ptotal-10;
		
			cout<<"The value of your ace has been changed from 11 to 1 in order to avoid busting"<<endl;
		}
	
	
		if(pnewcard == 11 && ptotal+pnewcard>21){
				pnewcard=1;
	
			cout<<"You were delt an ace. The value of ace has been changed from 11 to 1 in order to avoid busting"<<endl;		
		}
	
	
	
		cout<<"Your new card is :   "<<pnewcard<<endl; 
	
			ptotal=ptotal+pnewcard;
		
		cout<<"Your new total is:   "<<ptotal<<endl;
	
		if (ptotal>21){
			cout<<"You have Busted!!"<<endl;
				state =  RESET;
		}  //end if
	
		if(pdeal>11){
			cout<<"You have Busted!!"<<endl;
				state =  RESET;
		}  //end if
		
		cout<<"Would you like to Hit or Stand?"<<endl;
		cin>>go;
	
		if (go == 'H'|| go == 'h'){
				state = HIT;
		}
		
		if (go == 's'|| go == 'S'){
				state = STAND;
		}


	break;


You left out BUST also by the way, or I just can't see it through the jungle. Turning the conditions into functions would go a long way toward making your code more readable and making it easier for you find where you made a mistake.

1
2
3
4
5
6
7
8
9
switch(state)
{
case 'H':
	hit();
	break;
case 'S':
	stand();
	break;
}


for example.

There's no reason to even have INITIAL, RESET, or BUST as one of the conditions either as they are all unrelated.
Thats really good advice. I will use it in the future! Thanks!
Topic archived. No new replies allowed.