Card Game Printing Incorrect

I'm trying to display King, Jack, Queen, Ace for cards that have a score of 10 or 11, because otherwise the player won't know what there card is as 10 = a king, jack, or queen. I've tried to do this by using an IF statement but something isn't quite right. Sometimes I get the correct result and other times I don't, when I test the program sometimes I get the result of "Dealers first card Clubs 11 Jack" Which is wrong because a Jack is only = a score of 10. And a score of 11 is = Ace, so it should be displaying "Dealers first card Clubs 11 Ace"... Another example is "Players first card Spades 10 Ace".. Again it shouldn't be saying Ace, it should be saying either a Jack, King or Queen. But I can't work out why it's doing this? Can anybody help

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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
 #include "pch.h"
#include <iostream>
#include <algorithm> 
#include <ctime>
#include <cstdlib>


using namespace std;
#define NUMBER_OF_CARDS 52


enum SUIT { HEARTS, DIAMONDS, CLUBS, SPADES };
enum FACES { ACE, JACK, KING, QUEEN };

static const char *SUIT_STRING[] = {
	"Hearts", "Diamonds", "Clubs", "Spades",

};

static const char *FACE_STRING[] = {
	"Ace", "Jack", "King", "Queen",

};


struct Card {
	SUIT suit; // H, D, S, C
	int value; // A, 2, 3, 4, 5, 6, 7, 8, 9, 10
	int score; // A = 1 2 = 2, Q = 10
	FACES face; //ACE, JACK, KING, QUEEN

};

Card Pack[NUMBER_OF_CARDS];

Card Playerscard[5];
Card Dealercard[5];

void SetupPack();
void SetupHearts();
void SetupDiamonds();
void SetupClubs();
void SetupSpades();
void displayPack();
char Menuchoice;
void Play(); //

void Menu();
void NewGame();

void Shuffle();
void Deal();
int PlayerScore = 0;
int DealerScore = 0;

int cardcount = 0;



int main()
{	
	Menu();
}


void Menu()
{
	do
	{
		cout << endl;

		cout << "1. New Game - Press N" << endl;
		

		cin >> Menuchoice; cout << endl;

		cout << "You entered: " << Menuchoice; cout << endl;


		if ((Menuchoice == 'N') || (Menuchoice == 'n'))
		{
			cout << endl;
			NewGame();

		}

void NewGame()
{

	cout << "New Game << endl;

	Play();
}


void Play()

{
	PlayerScore = 0;
	DealerScore = 0;
	SetupPack();
	Shuffle();

	Deal();

	cout << endl;

	if (PlayerScore > 21)
	{
		cout << "You went bust, dealer wins";
		cout << endl;

	}

	if (DealerScore > 21)
	{
		cout << "Player wins, dealer went bust";

		cout << endl;

		
	}


void SetupPack()
{
	SetupHearts();
	SetupDiamonds();
	SetupClubs();
	SetupSpades();

}

void SetupHearts()
{
	for (int loop = 0; loop <= 12; loop++)
	{
		Pack[loop].suit = HEARTS;
		Pack[loop].value = loop + 1;
		Pack[loop].score = loop + 1;

	}
	//score for Ace, Jack, Queen, King
	Pack[0].score = 11;
	Pack[10].score = 10;
	Pack[11].score = 10;
	Pack[12].score = 10;
	//face
	Pack[0].face = ACE;
	Pack[10].face = KING;
	Pack[11].face = JACK;
	Pack[12].face = QUEEN;

}

void SetupDiamonds()
{
	for (int loop = 13; loop <= 25; loop++)
	{
		Pack[loop].suit = DIAMONDS;
		Pack[loop].value = loop + 1 - 13; 
		Pack[loop].score = loop + 1 - 13;

	}
	//score for Jack, Queen, King
	Pack[13].score = 11;
	Pack[23].score = 10;
	Pack[24].score = 10;
	Pack[25].score = 10;

	//face
	Pack[13].face = ACE;
	Pack[23].face = KING;
	Pack[24].face = JACK;
	Pack[25].face = QUEEN;
}


void SetupClubs()
{
	for (int loop = 26; loop <= 38; loop++)
	{
		Pack[loop].suit = CLUBS;
		Pack[loop].value = loop + 1 - 25; 
		Pack[loop].score = loop + 1 - 25; 
	}
	//score for Ace, Jack, Queen, King
	Pack[26].score = 11;
	Pack[36].score = 10;
	Pack[37].score = 10;
	Pack[38].score = 10;


	//face
	Pack[26].face = ACE;
	Pack[36].face = KING;
	Pack[37].face = JACK;
	Pack[38].face = QUEEN;
}

void SetupSpades()
{
	for (int loop = 39; loop <= 51; loop++)
	{
	Pack[loop].suit = SPADES;
	Pack[loop].value = loop + 1 - 38; 
	Pack[loop].score = loop + 1 - 38; 

	}
	//score for Ace, Jack, Queen, King
	Pack[39].score = 11; //ace
	Pack[49].score = 10;
	Pack[50].score = 10;
	Pack[51].score = 10;

	//face

	Pack[39].face = ACE;
	Pack[49].face = KING;
	Pack[50].face = JACK;
	Pack[51].face = QUEEN;
}

void displayPack()

{
	for (int loop = 0; loop < NUMBER_OF_CARDS; loop++)
	{
		cout << "Suit =  " << Pack[loop].suit;
		cout << " Score = " << Pack[loop].score;
		cout << " Value = " << Pack[loop].value;

		cout << endl;
	}
}

void Shuffle()
{
	unsigned seed = time(0);
	srand(seed);

	for (int Shuffle = 0; Shuffle < 100; Shuffle++)
	{
		int RandomNum1 = rand() % 52;
		int RandomNum2 = rand() % 52;

		Card CardTemp = Pack[RandomNum1];
		Pack[RandomNum1] = Pack[RandomNum2];
		Pack[RandomNum2] = CardTemp;
	}
}


void Deal()
{
Playerscard[0] = Pack[0];

cout << "Players first card " << SUIT_STRING[Playerscard[0].suit] << Playerscard[0].score << endl;


if (Playerscard[0].score == 10) {
cout << FACE_STRING[Playerscard[0].face] << endl;
}


if (Playerscard[0].score == 11) {
cout << FACE_STRING[Playerscard[0].face] << endl;
}
cout << endl;

Dealercard[0] = Pack[1];

cout << "Dealers first card " << SUIT_STRING[Dealercard[0].suit] << Dealercard[0].score << endl;


if (Dealercard[0].score == 10)
cout << FACE_STRING[Dealercard[0].face] << endl;



if (Dealercard[0].score == 11)
cout << FACE_STRING[Dealercard[0].face] << endl;


cout << endl;

Playerscard[1] = Pack[2];

cout << "Players second card " << SUIT_STRING[Playerscard[1].suit] << Playerscard[1].score << endl;

if (Playerscard[1].score == 10)
cout << FACE_STRING[Playerscard[1].face] << endl;


if (Playerscard[1].score == 11)
cout << FACE_STRING[Playerscard[1].face] << endl;


cout << endl;

Dealercard[1] = Pack[3];

cout << "Dealers second card " << SUIT_STRING[Dealercard[1].suit] << Dealercard[1].score << endl;

if (Dealercard[1].score == 10)
cout << FACE_STRING[Dealercard[1].face] << endl;


if (Dealercard[1].score == 11)
cout << FACE_STRING[Dealercard[1].face] << endl;

cout << endl;

PlayerScore = PlayerScore + Playerscard[0].score + Playerscard[1].score; 
cout << endl;

cout << "Players score is: " << PlayerScore << endl;


DealerScore = DealerScore + Dealercard[0].score + Dealercard[1].score; 
cout << endl;

cout << "Dealers score is: " << DealerScore << endl;


}


 
Last edited on
Have you even compiled this?

Line 86: You're missing 2 close braces and a while. One for the loop and one for the function.

Line 90: Missing a close quote.

Line 124. Missing a close }

Lines 150-152: While the order of the face cards doesn't matter, conventionally these are assigned JACK, QUEEN, KING.

Line 240: srand() should be called ONCE in main. Do not call it each time you shuffle.

Line 242. Do not name your loop variables the same as your function name. The compiler knows the difference, but it's confusing to the reader.

You have a lot of repetitive code. You should learn how to pass arguments. e.g. SetupHearts(), SetupDiamonds(), SetupClubs() and setupSpades() can all be replaced with a single SetupSuit (SUIT suit) function.






Last edited on
I haven't posted the entire code, so yes it does compile, I only posted the code that would need to be seen in order to fix the problem.
Why would you post something with missing closing braces and missing closing quotes?
Post something that compiles, at least!
it looks to me like you just need something along the lines of

for(all the cards in the players hand)
cout card.suit << card.face << card.value
cout total values as well?

possibly in a displayhand function?

the logic is too complicated for what you are trying to do.
loop, until player stops adding cards or busts.
display player's cards
run the dealer's hand in a loop until whatever rules to stop (win, hit 17? bust?)
print the winner.
Last edited on
I only posted the code that would need to be seen in order to fix the problem

The problem with this line of thinking is that it's much easier for us to find a problem when you post code that compiles. That way we can compile, run, and debug the code ourselves. Otherwise we have to try to find the problem purely by inspection.

You have 66 lines of code (258-323) just to print a card. That should be a red flag that something is wrong.

Don't store score and face. Those are redundant values and where there is redundancy, there's the opportunity to make the data inconsistent. For example, you only store the face value for cards that need it. Otherwise it's uninitialized. It would be too easy to mess up and access the face value when it isn't set.

So I'd store the suit and the value (1-13) only.
Create a score method to return the score:
int card::score() { return std::max(value, 10); }

Add methods to get the name of the card and the suit:
1
2
3
4
5
6
7
8
9
const char * Card::suitName() {
    static const char *names[] = {"Hearts", "Diamonds", "Clubs", "Spades"};
    return names[suit];
};
const char *Card::valueName() {
    static const char *names[] = {"Ace", "Two", "Three", "Four", "Five", "Six", "Seven",
        "Eight", "Nine", "Ten", "Jack", "Queen", "King" };
    return names[value-1];  // Values run 1-13, indexes are 0-12
};

Topic archived. No new replies allowed.