-2

I'm currently trying to make a simple Blackjack program that is single player and only has cards from 1-10. The issue that I'm having is that whenever I ask for a new card for the third time, it doesn't total up my cards properly. The program also doesn't restart properlyHere is an example of the output:

Welcome to Blackjack!
Here are your first cards: 10, 2
Your total is 12.
Would you like to draw another card? (y/n)
y
You got a new card with a value of 6.
Your total is now 18.
Would you like to draw another card? (y/n)
y
You got a new card with a value of 4.
Your total is now 16.
Would you like to draw another card? (y/n)
n
Welcome to Blackjack!
Here are your first cards: 6, 4
Your total is 10.
Would you like to draw another card? (y/n)

I have tried using a do-while loop so that it asks for a new card whenever the total < 21 but it ends up the same way. I'm at a loss at how to solve this.

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int getCard(int);

int main()
{
    srand(time(0));
    int total, card1, card2, newTotal = 0;
    char choice, playAgain = 'y';

    while (playAgain == 'y')
    {
        cout << "Welcome to Blackjack!\n";
        card1 = rand() % 10 + 1;
        card2 = rand() % 10 + 1;
        total = card1 + card2;
        cout << "Here are your first cards: " << card1 << ", " << card2 << endl;
        cout << "Your total is " << total << "." << endl;

        cout << "Would you like to draw another card? (y/n)\n";
        cin >> choice;

         while (choice == 'y')
         {
             newTotal = getCard(total);

             if (newTotal > 21)
             {
                 cout << "Bust!" << endl;
                 choice = 'n';
             }

             else if (newTotal < 21)
             {
                cout << "Would you like to draw another card? (y/n)\n";
                cin >> choice;
             }
             else
             {
                 cout << "Congratulations, you won!\n";
                 choice = 'n';
             }
         }
      }

    cout << "Would you like to play again? (y/n)\n";
    cin >> playAgain;

    return 0;
}

int getCard(int total)
{ 
    int addCard, newTotal;
    addCard = rand() % 10 + 1;
    newTotal = total + addCard;
    cout << "You got a new card with a value of " << addCard << "." << 
    endl;
    cout << "Your total is now " << newTotal << "." << endl;
    return newTotal;
}

The program is expected to start the player off with 2 cards and then ask them if they'd like to draw another one. If they input 'y' then it would do so and retotal the cards up. If they say no, then the game just ends. If they say yes and the new cards makes the total > 21 then it returns "bust." If it makes the total become 21 then it tells the player that it wins. At the end of each game, it should ask the player if they would like to play again.

Oheyaj
  • 41
  • 3
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Jesper Juhl Feb 06 '19 at 05:10
  • I am not sure it is a debugger issue. It is not a bug, but a wrong logic. Oheyaj is a new user, and probably learning how to code. There is clearly an attempt to solve the problem here (i.e. he actually wrote code that compiles), so I think it is nicer to give him a hand rather than closing his question. – JonasVautherin Feb 06 '19 at 06:47
  • @JonesV "Wrong logic" is what bugs are. – melpomene Feb 06 '19 at 07:00
  • Right, I may not have the correct wording, that's a good point. But downvoting the question and saying "just fix your damn code!" is not super constructive for a beginner. A debugger will be of no help here. What is StackOverflow made for, in the end? Reminding people that they can read documentation and fix their code on their own, or trying to help? It's not like the question was "pleaz write me a blackjack code thx", is it? This question is super clear and well formed. Let's try to be welcoming to new users. – JonasVautherin Feb 06 '19 at 07:21
  • 1
    Yes, I am a beginner C++ programmer and still clearly have holes in my logic. Thank you for pointing out my mistakes very kindly JonesV. You really helped! – Oheyaj Feb 06 '19 at 07:33
  • @JonesV I would say neither. StackOverflow was made to be a repository of specific questions and answers. "Here's my code; what's wrong with it?" is not a useful question because it doesn't generalize; it's unlikely to be helpful to other people, even in principle: Even if someone were to have exactly the same problem, they would not find this question through a search. That is why we encourage people to create a [mcve]: Either they'll figure it out on their own in the process, or they end up with a better, more focused question. – melpomene Feb 06 '19 at 18:49

1 Answers1

0

You have issues in your logic.

  • First, newTotal = getCard(total); should be total = getCard(total);, so that total gets updated (so you should replace newTotal by total everywhere). By using this newTotal variable, you always discard the previous total in the while loop and start from the one that was defined as total = card1 + card2; before the loop.

  • Second, while (playAgain == 'y') is always true because you don't update playAgain from inside this loop (so you never reach the code that asks if the user wants to play again). Move the following two lines inside this loop:

cout << "Would you like to play again? (y/n)\n";
cin >> playAgain;

Like so:

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int getCard(int);

int main()
{
    srand(time(0));
    int total, card1, card2, newTotal = 0;
    char choice, playAgain = 'y';

    while (playAgain == 'y')
    {
        cout << "Welcome to Blackjack!\n";
        card1 = rand() % 10 + 1;
        card2 = rand() % 10 + 1;
        total = card1 + card2;
        cout << "Here are your first cards: " << card1 << ", " << card2 << endl;
        cout << "Your total is " << total << "." << endl;

        cout << "Would you like to draw another card? (y/n)\n";
        cin >> choice;

         while (choice == 'y')
         {
             total = getCard(total);

             if (total > 21)
             {
                 cout << "Bust!" << endl;
                 choice = 'n';
             }

             else if (total < 21)
             {
                cout << "Would you like to draw another card? (y/n)\n";
                cin >> choice;
             }
             else
             {
                 cout << "Congratulations, you won!\n";
                 choice = 'n';
             }
         }

        cout << "Would you like to play again? (y/n)\n";
        cin >> playAgain;
    }

    return 0;
}

int getCard(int total)
{
    int addCard, newTotal;
    addCard = rand() % 10 + 1;
    newTotal = total + addCard;
    cout << "You got a new card with a value of " << addCard << "." << endl;
    cout << "Your total is now " << newTotal << "." << endl;
    return newTotal;
}
JonasVautherin
  • 7,297
  • 6
  • 49
  • 95