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);
}
elseif (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.
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 ;-)