Program displaying unexpected total of 1

Hi, I'm putting together a Blackjack program (yes it's homework, no I'm not looking for answers, just a hint or pointer in the right direction.

The issue is that the player_total variable gets displayed as 1 as part of the cout statement in the win loop. This obviously makes no sense, because the player_total has to equal 21 to even enter into the loop in the first place, so it should display 21. Prior iterations of the hands will correctly show the total (so it might be 13, then 19, and then suddenly it's 1). What's also odd is that my win loop is basically a mirror of my bust loop, but the bust loop displays the total just fine.

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
int main()
{
	//Define variables
	int player_card1
		, player_card2
		, player_card_hit	//Separate variable for the card hit value
		, player_total;
		
		//Use named constants for max and min card values
	const int minvalue = 1
		, maxvalue = 10;

	char hit
		, repeat_game = 'Y';  //Initializing repeat value to begin game

	while (repeat_game == 'Y' || repeat_game == 'y')
	{
		player_total = 0;	//Initialize total to 0 at start of game
		
		//Generate initial card values and total
		srand(time(0));
		player_card1 = (rand() % (maxvalue - minvalue) + 1) + minvalue;  //Restricting card values between 1 and 10
		player_card2 = (rand() % (maxvalue - minvalue) + 1) + minvalue;
		player_total = player_card1 + player_card2;

		//Display card values and total
		cout << "First cards: " << player_card1 << ", " << player_card2 << endl;
		cout << "Total: " << player_total << endl;
		cout << "Do you want another card? (y/n)" << endl;
		cin >> hit;

		//HIT loop: repeat as long as player hits
		while (hit == 'Y' || hit == 'y')
		{
			//Generate hit card value and new total
			player_card_hit = (rand() % (maxvalue - minvalue) + 1) + minvalue;
			player_total = player_total + player_card_hit;
			
			//KEEP PLAYING loop as long as total is less than 21
			while (player_total < 21 & (hit == 'Y' || hit =='y')) //Have to account for both the total value and whether player wants to hit
			{
				//Display hit card value and new total
				cout << "Card: " << player_card_hit << endl;
				cout << "Total: " << player_total << endl;
				cout << "Do you want another card? (y/n)" << endl;
				cin >> hit;

				//Recalculate player total to determine which loop triggers next
				player_card_hit = (rand() % (maxvalue - minvalue) + 1) + minvalue;
				player_total = player_total + player_card_hit;
			}
			
			//BUST LOOP when total is greater than 21
			while (player_total > 21 & (hit == 'Y' || hit == 'y'))
			{
				//Display card value, total, and bust message
				cout << "Card: " << player_card_hit << endl;
				cout << "Total: " << player_total << endl;
				cout << "Bust." << endl;

				//Reset hit and total values to end bust loop
				player_total = 0;
				hit = 'n';
			}

			//WIN LOOP if total = 21 and player wins
			while (player_total = 21 & (hit == 'Y' || hit == 'y'))
			{
				//Display card value, total, and win message
				cout << "Card: " << player_card_hit << endl;
				cout << "Total: " << player_total << endl;
				cout << "Win." << endl;

				//Reset hit and total values to end bust loop
				player_total = 0;
				hit = 'n';
			}
		}
		//Ask if player wants to repeat
		cout << "Would you like to play again? (y/n)" << endl;
		cin >> repeat_game;
	}
	return 0;
}


Actual output:
First cards: 4, 9
Total: 13
Do you want another card? (y/n)
y
Card: 6
Total: 19
Do you want another card? (y/n)
y
Card: 2
Total: 1 *HERE IS THE ISSUE, THIS SHOULD DISPLAY 21*
Win.
Would you like to play again? (y/n)

Last edited on
1
2
//WIN LOOP if total = 21 and player wins
while (player_total = 21 & (hit == 'Y' || hit == 'y'))


You are assigning player_total to (21 bitwise& (1 or 0)), i think you meant to use == and &&?

while(player_total == 21 && (hit == 'Y' || hit == 'y'))
Last edited on
AAAARRRRGGGGGHHHHHH

What I really meant to do was determine it if equals 21, not assign 21 as a value.

The assignments vs. comparison operators are driving me nuts, I'm coming from SQL so "=" makes the comparison in my head. Thanks for catching that.
Last edited on
Topic archived. No new replies allowed.