Trouble getting "Bust" output in a blackjack program

I have everything working in this except when the user goes over 21, the "Bust! You lose!" prompt doesn't come up. I just included the relevant code.



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
 int userHandValue = players[0].handValue();
    string hitOrStay = "";
    
        
    if (userHandValue == 21 && players[0].getHand().size()) {
        cout << "Blackjack! You won!" << endl;
        return;
    }
    while (userHandValue <= 21 && players[0].getHand().size() && hitOrStay != "stay") {
        
        if (userHandValue > 21) {
            cout << "Bust! You lose!" << endl;
        }
        if (userHandValue <= 21) {
            printPlayers(true); // Show hole card
            cout << "hit or stay: ";
            cin >> hitOrStay;
            
            if (hitOrStay == "hit") {
                dealCard(0);
            }
            else if (hitOrStay == "stay")
            {
                handleDealersPlay();
            }
        }

    }
Last edited on
Your test for value > 21 is inside a loop that only continues as long as value <= 21. So you need to put that > 21 test after that loop.

What's up with the testing of the hand size?
Last edited on
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
int userHandValue = players[0].handValue();
string hitOrStay = "";

while (userHandValue < 21 && players[0].getHand().size() && hitOrStay != "stay") 
{
                // You don't need to check if(userHandValue < 21)
                // If it is NOT the case you will be outside while loop
                // do you understand 
                // userHandValue >= 21 the next code will not be execute
 		printPlayers(true); // Show hole card
		cout << "hit or stay: ";
		cin >> hitOrStay;

		if (hitOrStay == "hit") {
			dealCard(0);
		}
		else if (hitOrStay == "stay")
		{
			handleDealersPlay();
		}
}

if (userHandValue == 21 && players[0].getHand().size()) {
	cout << "Blackjack! You won!" << endl;
	return;
}

// As @dutch offer you put it outside the loop
if (userHandValue > 21) {
	cout << "Bust! You lose!" << endl;
	return;
}


I just included the relevant code.


Probably dealCard(0) change userHandValue. But sometimes you forget that your code is clear for you, because you write it, for outsiders it can be mess if you offer us only snippet from it.
Last edited on
vroll,

You probably have and others bugs too, but you don't know about them yet.

1. Ask yourself what happen when Dealer and Player have Blackjack or a tie?

"If both the player and the dealer have a tie—including with a blackjack—the bet is a tie or “push” and money is neither lost, nor paid."
"If both the dealer and player bust, the player loses."


2. I'm also interested in what rules follow the dealer?

3. As, dutch, I also don't understand what is about players[0].getHand().size() testing?

You could have released the whole code here. So we'd clear it from bugs and you can teach something. Such a game here most of us can create while they sleep. dutch will write this code even unconscious :-)

Of course, if you do not intend to make the money with it. Then you can keep it ;-)
Last edited on
Topic archived. No new replies allowed.