C++ BlackJack Stuck trying to program Ace

You can implement it both ways.

To add 10 later, you can change your total_hand() function to become:

int total_hand
{
    int count = 0;
    int num_of_aces = 0;
    for (int i = 0; i < hand.size(); i++)
    {
        card c = hand[i];
        if (c.get_value() == 11){
            num_of_aces += 1;
            count += 1;
        }
        else
            count = count + c.get_value();
    }
    for (int i = 0; i < num_of_aces; i++){
        if (count + 10 <= 21)
            count += 10;
    return count;
}

To subtract 10 later (as your intuition suggests), you can do the following:

int total_hand
{
    int count = 0;
    int num_of_aces = 0;
    for (int i = 0; i < hand.size(); i++)
    {
        card c = hand[i];
        if (c.get_value() == 11)
            num_of_aces += 1;

        count = count + c.get_value();
    }

    while(count > 21 && num_of_aces--)
        count -=10;

    return count;
}

Leave a Comment