Fixing logic errors

Hello. This is a program for blackjack in C++. Most of the needed parts are in here like a normal program, but there are a lot of logic errors I do not know how to fix. Anyways, the getRand() function was provided by the teacher and is not supposed to be changed. The only reason why the <ctime> library is here is the fact that it is used in that function. Therefore my knowledge is only limited to the <iostream>, <cmath>, and <string> libraries. The biggest logic error I have is the bool instawinner(int check); and bool botInstawinner(int check); are not checking if the hand is a blackjack or a bust unless the user hits again. I do not understand what is happening specifically here and need help. (this is code after multiple attempts of trying to fix 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
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
  #include <iostream>
#include <ctime>
#include <string>
static int i = -1;
using namespace std;
string name();
int getRand();
int firstTurn(int first);
void dealer(int& dNew, int& dOld);
void player(int& pNew, int& pOld);
bool hs();
bool botHs(int dealer);
void card(int card);
int specCard(int num);
bool instawinner(int check);
bool botInstawinner(int check);
int main()
{
	int dealerN = 0, dealerO = 0, playerN = 0, playerO = 0;
	string playerName = name();
	dealerO = firstTurn(dealerO);
	player(playerN, playerO);
	dealer(dealerN, dealerO);
}
string name()	//this is a pointless function needed for the assignment
{
	string name;
	cout << "Welcome to the Trump Casino!\nPlease enter your name: ";
	cin >> name;
	return name;
}
int getRand()	//this is the random function (i do not know how this works. This is what the teacher provided.
{
	if(i == 100) i = 0;
	int rands[100];
	srand((unsigned)time(0));

	for(int index=0; index<100; index++)
	{ 
		rands[index] = (rand()%13)+1; 
	}

	i++;

	int n = rands[i];
	return n;
}
int firstTurn(int first)//this represents the first turn in the game
{
	int temp, reg;
	temp = getRand();
	reg = temp;
	cout << "\nThe dealer:\n? + " << temp << endl;
	temp += getRand();
	return temp;
}
void dealer(int& dNew, int& dOld) //this represents the bot dealers turn
{
		int temp;
	
	if(dOld == 0)
	{
		temp = getRand();
		dNew = getRand();
		cout << "\nDealer: \n";
		card(temp);
		cout <<"+ \n";
		card(dNew);
		dNew = specCard(dNew);
		temp = specCard(temp);
		dOld = dNew + temp;
	}
	if (botHs(dOld)&&botInstawinner(dOld))
	{
		do
		{
			dNew = getRand();
			cout << "\nDealer: \n";
			cout << dOld << endl;
			cout <<"+ \n";
			card(dNew);
			dNew = specCard(dNew);
			dOld = dNew + dOld;
		}
		while (botHs(dOld)&&botInstawinner(dOld));
	}
}
void player(int& pNew, int& pOld) //this represents the players turn
{
	int temp;
	
	if(pOld == 0)
	{
		temp = getRand();
		pNew = getRand();
		cout << "\nPlayer: \n";
		card(temp);
		cout <<"+ \n";
		card(pNew);
		pNew = specCard(pNew);
		temp = specCard(temp);
		pOld = pNew + temp;
	}
	if (hs()&&instawinner(pOld))
	{
		do
		{
			pNew = getRand();
			cout << "\nPlayer: \n";
			cout << pOld << endl;
			cout <<"+ \n";
			card(pNew);
			pNew = specCard(pNew);
			pOld = pNew + pOld;
		}
		while (hs()&&instawinner(pOld));
	}
}
bool hs()//this will ask the user to hit or stand and return true or false
{
	char response;
	do
	{
		cout << "Hit(h) or Stand(s)? : ";
		cin >> response;
	}
	while (response != 'h' && response != 's' && response != 'H' && response != 'S');
	if (response == 'h' || response == 'H')
		return 1;
	else
		return 0;
}
bool botHs(int card)	//this will check whether the bot will keep going or not
{
	if (card > 17)
		return 0;
	else if (card <= 17)
		return 1;
	else if (card == 21)
		return 0;
}
void card(int card) //this will display the correct symbol for the card
{
		switch(card)
		{
		case 1:
			cout << "A" << endl;
			break;
		case 2:
			cout << card << endl;
			break;
		case 3:
			cout << card << endl;
			break;
		case 4:
			cout << card << endl;
			break;
		case 5:
			cout << card << endl;
			break;
		case 6:
			cout << card << endl;
			break;
		case 7:
			cout << card << endl;
			break;
		case 8:
			cout << card << endl;
			break;
		case 9:
			cout << card << endl;
			break;
		case 10:
			cout << card << endl;
			break;
		case 11:
			cout << "J" << endl;
			break;
		case 12:
			cout << "Q" << endl;
			break;
		case 13:
			cout << "K" << endl;
			break;
		default:
			cout << card << endl;
		}
}
int specCard(int num)//changes 11-13 into 10 since they represent J, Q, K
{
	if (num > 10 && num <= 13)
	{
		num = 10;
	}
	return num;
}
bool instawinner(int check)//this checks whether the user won or not
{
	if (check < 21)
		return true;
	else if (check == 21)
	{
		cout << "You win! Here's NOTHING" << endl;
		return false;
	}
	else if(check > 21)
	{
		cout << "You busted with a "<< check<< " mate." << endl;
		return false;
	}
}
bool botInstawinner(int check)	//this checks whether the bot won or not
{
	if (check <= 17)
		return true;
	else if (check == 21)
	{
		cout << "Dealer wins! He's too pro for you." << endl;
		return false;
	}
	else if(check > 21)
	{
		cout << "Dealer busted with a "<< check<< " mate. You win!" << endl;
		return false;
	}
}
Last edited on
finding this difficult to read, but...
your random function returns a number between 1 and 13, which makes sense in terms of the cards in a suit, but if the number returned is 11, 12 or 13, shouldn't the value of those cards be 10? i.e. the picture cards.

the other thing you could do is add this to instawinner() on line 199:
cout << "** value of check in instawinner: " << check << endl;

and do the equivalent in botInstawinner() too.

Do the values printed out correspond with what's output on the screen in terms of your card deals?
Last edited on
As of your first question. My function void card(int card) is used to display the 11, 12, and 13 as a J, Q, and K. Later int specCard(int num) will change these to the value of 10.

As for the other thing
the other thing you could do is add this to instawinner() on line 199:
cout << "** value of check in instawinner: " << check << endl;


I do not understand the point of this (sorry).

As for the last question the answer is yes as for the void function I have void card(int card).

Sorry for the illegibility.

I just made a extra version so my old question I was able to fix (after like 4 hours) but my new one is getting my program to be able to let an ace start with the value of 11 but if it makes the hand value go above 21, it will automatically change it to 1. If you have questions about my code due to illegibility, you can ask about specifics through my e-mail monkey.pancakeman@gmail.com

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
249
250
251
252
253
254
255
256
257
#include <iostream>
#include <ctime>
#include <string>
static int i = -1;
using namespace std;
string name();
int getRand();
int firstTurn(int first);
void dealer(int& dNew, int& dOld);
void player(int& pNew, int& pOld);
bool hs();
bool botHs(int dealer);
void card(int card);
int specCard(int num);
bool instawinner(int check);
bool botInstawinner(int check);
void winner (int dHand, int pHand);
int ace (int card, int hand);
int ace1Counter(int card, int counter);
void aceTo1(int card, int& counter, int& hand);
int main()
{
	int dealerN = 0, dealerO = 0, playerN = 0, playerO = 0;
	string playerName = name();
	dealerO = firstTurn(dealerO);
	player(playerN, playerO);
	if (playerO <= 21)
		dealer(dealerN, dealerO);
	if(playerO < 21 && dealerO < 21)
		winner (dealerO, playerO);
}
string name()	//this is a pointless function needed for the assignment
{
	string name;
	cout << "Welcome to the Trump Casino!\nPlease enter your name: ";
	cin >> name;
	return name;
}
int getRand()	//this is the random function (i do not know how this works. This is what the teacher provided.
{
	if(i == 100) i = 0;
	int rands[100];
	srand((unsigned)time(0));

	for(int index=0; index<100; index++)
	{ 
		rands[index] = (rand()%13)+1; 
	}

	i++;

	int n = rands[i];
	return n;
}
int firstTurn(int first)//this represents the first turn in the game
{
	int temp, reg;
	temp = getRand();
	reg = temp;
	cout << "\nThe dealer:\n? + " << endl;
	card(temp);
	temp = specCard(temp);
	temp += getRand();
	return temp;
}
void dealer(int& dNew, int& dOld) //this represents the bot dealers turn
{
	while(botInstawinner(dOld)&&botHs(dOld))
	{
		int aceCounter = 0;
		dNew = getRand();
		cout << "\nDealer: \n";
		cout << dOld << endl;
		cout <<"+ \n";
		card(dNew);
		dNew = specCard(dNew);
		dNew = ace(dNew, dOld);
		dOld = dNew + dOld;
		aceTo1(dNew, aceCounter, dOld);
	}
}
void player(int& pNew, int& pOld) //this represents the players turn
{
	int temp, temp1, aceCounter = 0;
	
	if(pOld == 0)
	{
		temp = getRand();
		pNew = getRand();
		cout << "\nPlayer: \n";
		card(temp);
		cout <<"+ \n";
		card(pNew);
		pNew = specCard(pNew);
		temp = specCard(temp);
		temp1 = temp + pNew;
		aceCounter = ace1Counter(pNew, aceCounter); 
		pNew = ace(pNew, temp1);
		temp = ace(temp, temp1);
		pOld = pNew + temp;	
		aceTo1(pNew, aceCounter, pOld);
	}
	while (instawinner(pOld)&&hs())
	{
		pNew = getRand();
		cout << "\nPlayer: \n";
		cout << pOld << endl;
		cout <<"+ \n";
		card(pNew);
		pNew = specCard(pNew);
		pNew = ace(pNew, pOld);
		pOld = pNew + pOld;
		aceTo1(pNew, aceCounter, pOld);
	}
}
bool hs()//this will ask the user to hit or stand and return true or false
{
	char response;
	do
	{
		cout << "Hit(h) or Stand(s)? : ";
		cin >> response;
	}
	while (response != 'h' && response != 's' && response != 'H' && response != 'S');
	if (response == 'h' || response == 'H')
		return 1;
	else
		return 0;
}
bool botHs(int card)	//this will check whether the bot will keep going or not
{
	if (card > 17)
		return 0;
	else if (card <= 17)
		return 1;
	else if (card == 21)
		return 0;
}
void card(int card) //this will display the correct symbol for the card
{
		switch(card)
		{
		case 1:
			cout << "A" << endl;
			break;
		case 2:
			cout << card << endl;
			break;
		case 3:
			cout << card << endl;
			break;
		case 4:
			cout << card << endl;
			break;
		case 5:
			cout << card << endl;
			break;
		case 6:
			cout << card << endl;
			break;
		case 7:
			cout << card << endl;
			break;
		case 8:
			cout << card << endl;
			break;
		case 9:
			cout << card << endl;
			break;
		case 10:
			cout << card << endl;
			break;
		case 11:
			cout << "J" << endl;
			break;
		case 12:
			cout << "Q" << endl;
			break;
		case 13:
			cout << "K" << endl;
			break;
		default:
			cout << card << endl;
		}
}
int specCard(int num)//changes 11-13 into 10 since they represent J, Q, K
{
	if (num > 10 && num <= 13)
	{
		num = 10;
	}
	return num;
}
bool instawinner(int check)//this checks whether the user won or not
{
	if (check < 21)
		return true;
	else if (check == 21)
	{
		cout << "You win! Here's NOTHING" << endl;
		return false;
	}
	else if(check > 21)
	{
		cout << "You busted with a "<< check<< " mate." << endl;
		return false;
	}
}
bool botInstawinner(int check)	//this checks whether the bot won or not
{
	if (check <= 17)
		return true;
	else if (check == 21)
	{
		cout << "Dealer got blackjack. He wins! He's too pro for you." << endl;
		return false;
	}
	else if(check > 21)
	{
		cout << "Dealer busted with a "<< check<< " mate. You win!" << endl;
		return false;
	}
}
void winner (int dHand, int pHand)
{
	if (dHand>pHand)
		cout << "You lost to the dealer since he had " << dHand <<" while you had " << pHand <<endl;
	else if (dHand<pHand)
		cout << "You won against the dealer since he had " << dHand <<" while you had " << pHand <<endl;
	else
		cout << "Tie at " << dHand <<endl;
}
int ace (int card, int hand)
{
	if (card == 1)
		return 11;
	else
		return card;
}
int ace1Counter(int card, int counter)
{
	if (card == 1)
	{
		counter++;
		return counter;
	}
	else
		return counter;
}
void aceTo1(int card, int& counter, int& hand)
{
	if(counter >= 1 && (hand + card) > 21)
	{
		counter--;
		hand = hand - 10;
	}
}
Topic archived. No new replies allowed.