Blackjack game returning odd values

OK, I've spent hours trying to figure out what the issue is, but at this point I am just looking at the same stuff over and over. The game accepts a bet and them shows you one dealer card and then your cards. The first issue is that the dealer card is shown just fine, but the player cards come back with a symbol that corresponds with 204 and the ASCII table. If I choose to hit and receive another card, it then tells me my cards value is 26212(always the same amount).

If I play another game however, then the dealer cards come up with the same symbol and do not work anymore. I figure that the issue must be in the sumOfCardValues() and showCards(), but I just can't figure it out. I'll put in here the code for the Card.h, card.cpp and source.cpp, if anyone can get me pointed in the correct direction, I would appreciate it.


Card.h
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
 #pragma once

#include<ctime>
#include<stdlib.h>
#include<string>
using namespace std;

class Card
{
private:
	char face;
	char suit;
	short value;

	static bool randomizerSeeded;    //
public:
	// Constructor and Destructor
	Card();
	~Card();

	//behaviors
	string toString();
	bool flipAceToOne();

	//accessors and mutators
	inline char getSuit() { return suit; }
	inline char getFace() { return face; }
	inline short getValue() { return value; }
};


card.cpp
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
#include "Card.h"
#include <time.h>

bool Card::randomizerSeeded = false;

Card::Card()
{
	//seed randomizer only once for all card objects to ensure unique card
	if (!randomizerSeeded)
	{
		srand(time(NULL));
		randomizerSeeded = true;
		//create random number from 3 to 6 for suit
		short min = 3;
		short max = 6;
		suit = rand() % (max - min + 1) + min;

		//create random number from 2 to 14 for face value
		min = 2;
		max = 14;
		short number = rand() % (max - min + 1) + min;

		//if else statement to set face and number value
		if (number >=2 && number <=9)
		{
			value = number;
			face = number + 48;
		}
		else if (number == 10)
		{
			value = number;
			face = 'T';
		}
		else if (number == 11)
		{
			value = 10;
			face = 'J';
		}
		else if (number == 12)
		{
			value = 10;
			face = 'Q';
		}
		else if (number == 13)
		{
			value = 10;
			face = 'K';
		}
		else if (number == 14)
		{
			value = 11;
			face = 'A';
		}
		else
		{
			value = -1;
			face = 'E';
		}




	}
}


Card::~Card()
{
}

string Card::toString() 
{
	string output = "";
	output += suit;
	output += face;
	return output;
}

bool Card::flipAceToOne()
{
	if (value==11)
	{
		value = 1;
		return true;
	}
	else
	{
		return false;
	}
}


source.cpp
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
#include<iostream>
#include<string>
#include<conio.h>
#include "Card.h"
#include <vector> //Standard Template Library
using namespace std;

//protypes
string showCards( vector<Card> cards);
short sumOfCardValues(vector<Card> cards);
void playHand(short &cash);


//declare constant variables
int const EXIT_VALUE = 3;

//global varible
short cash = 100;


//Entry into main application

int main()
{
	cout << "Welcome to Chris' Blackjack Table!" << endl;
	cout << "\nYour starting balance is $" << cash << endl;

	//pause
	cout << "Press any key to continue..." << endl;
	_getch();

	short choice = 0;

	system("cls");  //clears console
	cout << "MENU\n" << endl;
	cout << "1) Play a hand" << endl;
	cout << "2) Check my balance" << endl;
	cout << "3) Exit" << endl;


	cout << "\nEnter your choice: ";
	cin >> choice;

	//initiate menu loop and switch block based on user choice
	do
	{
		switch (choice) {
		case 1:
			playHand(cash);
			break;
		case 2:
			cout << "\nChecking my balance..." << endl;

			break;
		case 3:
			cout << "\nGoodbye!" << endl;
			break;
		default:
			cout << "\nError, please choose from menu options." << endl;
			break;

		}
		//pause
		cout << "\nPress any key to continue..." << endl;
		_getch();
	} while (choice != EXIT_VALUE);


	return 0;
}

//show cards in the vector (resizable array)
string showCards( vector<Card>cards) 
{
	string output = " ";
	
	for (Card c : cards)
	{
		output += c.toString() + " ";
	}

	return output;
}

//Adds the total value of the cards
short sumOfCardValues(vector<Card>cards)
{
	short total = 0;

	for (Card c : cards)
	{
		total = total + c.getValue();
	}

	return total;
}

// play a hand of blackjack
void playHand(short &cash) 
{
	//create two ArrayLists to hold Card objects
	vector<Card> playerCards;
	vector<Card> dealerCards;
	short dealerCardsTotal = 0;
	short playerCardsTotal = 0;

	//get bet amount
	short wager = 0;
	cout << "\nEnter your wager amount: ";
	cin >> wager;

	//create two cards for the dealer to show the first one
	Card card1;
	Card card2;
	dealerCards.push_back(card1);
	dealerCards.push_back(card2);
	dealerCardsTotal = sumOfCardValues(dealerCards);

	cout << "\nDealer shows a: " << dealerCards[0].toString() << endl;

	//create two cards for the player and show them both
	
	playerCards.push_back(Card() );
	playerCards.push_back(Card() );
	playerCardsTotal = sumOfCardValues(playerCards);

	cout << "\nYour cards: " << showCards(playerCards) << endl;

	//loop until player stands: ('S')

	char answer = '?';

	do
	{
		cout << "\nDo you want to hit or stand (H/S)? ";
		cin.sync();
		cin >> answer;
		cin.sync();

		if (toupper(answer) == 'H')
		{
			//give a card to the player
			Card c;
			cout << "\nYou were dealt this card: " << c.toString() << endl;
			playerCards.push_back(c);  //adds card to players hand

			//sum up the card values
			playerCardsTotal = sumOfCardValues(playerCards);

			//did the player bust?
			if (playerCardsTotal > 21)
			{
				//do you have an Ace to flip value to a 1
				for (Card c : playerCards)
				{
					if (c.getValue() ==11)
					{
						cout << "\nYour total is " << playerCardsTotal << endl;
						c.flipAceToOne();
						cout << "However, you have an ace that was flipped to a '1' " << endl;
						playerCardsTotal = sumOfCardValues(playerCards);
						cout << "\nYour new total is " << playerCardsTotal << endl;

						//if good, break out of the loop.  Otherwise continue loop and look for Aces
						if (playerCardsTotal <= 21)
							break;
					}
				}
			}
			//show the cards and the total
			cout << "\nHere are your cards: " << showCards(playerCards) << endl;
			cout << "Your total is " << playerCardsTotal << endl;


			//if players busts, stop the loop
			if (playerCardsTotal > 21)
				answer = 'S';
		}
	} 
	while (toupper(answer) != 'S');

	//Determine whether player earns or losses money(if playercards > lose money, if playercards < earn money
	if (playerCardsTotal > 21)
	{
		cout << "\nYou busted!" << endl;
		cash = cash - wager; 
	}
	else
	{
		//if the player stands, make the dealer hit until reaching 17 or greater
		do
		{
			if (dealerCardsTotal < 17)
			{
				Card c;
				cout << "\nDealer was dealt: " << c.toString() << endl;
				dealerCards.push_back(c);
				cout << "\nDealer cards: " << showCards(dealerCards) << endl;
				dealerCardsTotal = sumOfCardValues(dealerCards);
				cout << "Dealer total: " << dealerCardsTotal << endl;
			}
		} 
		while (dealerCardsTotal < 17);

		//show the cards for the dealer and player
		cout << "\nYour cards: " << showCards(playerCards) << " (" << playerCardsTotal << " )" << endl;
		cout << "\nDealer cards: " << showCards(dealerCards) << " (" << dealerCardsTotal << " )" << endl;

		//determine winner
		if (dealerCardsTotal > 21)
		{
			cout << "\nThe dealer busts!" << endl;
			cash += wager; 
		}
		else if (dealerCardsTotal > playerCardsTotal)
		{
			cout << "\nDealer wins." << endl;
			cash -= wager;
		}
		else if (playerCardsTotal > dealerCardsTotal)
		{
			cout << "\nYou Win! " << endl;;
			cash += wager;
		}
		else
		{
			cout << "\nYour pushed the dealer (tie)" << endl;
		}
	}
	//Display players cash position
	cout << "Your cash balance is $" << cash << endl;
}
You forgot to convert your Card::suit variable to a printable value.
Try this:
1
2
3
4
5
6
7
8
9
10
11
string Card::toString() 
{
	std::string suit;
	switch( this->suit ){
	    case 3: suit = "Club";    break;
	    case 4: suit = "Diamond"; break;
	    case 5: suit = "Heart";   break;
	    case 6: suit = "Spade";   break;
	}
	return suit + face;
}


Also, you have a logical error in your Card::Card() method, here the fix:
1
2
3
4
5
6
7
	//seed randomizer only once for all card objects to ensure unique card
	if (!randomizerSeeded) 
	{
		srand(time(NULL));
		randomizerSeeded = true;
	}
         ...

Last edited on
The logical error was causing the entire issue. I may have never noticed that if you didn't point it out. The suit is returned as an actual club, diamond, heart or spade so there was no need in this case to use the switch statement. I think the short variable returns the associated symbol from the ASCII table. Because I didn't close my if statement, it was pulling the wrong numbers and not randomizing the output.

Thanks so much.
One other question. Any idea why my my switch block may not be functioning properly. Whichever case I select, the program stays in there and never breaks back out. So if choice ==1 I go to playHand(), but it stays in there. It'll break out and go to the _getch(); but then go right back to the previous selection. If I select to check my balance, it will show the balance, then say press any key to continue and go right back to showing my balance. It's like it is stuck in a loop for each case.
You need to change your 'choice' variable somewhere inside your loop.
Assuming your source.cpp from your initial post, you could move the do { from line 45 to 33.
Last edited on
ahhhhh... I see. I had the cin>>choice; outside of the do statement. With it written that way the user cannot input their choice for the menu options after the initial input. Thanks for the help.
You need to change your 'choice' man
Topic archived. No new replies allowed.