1

I am a beginner and I am not fully understanding what I am doing wrong using ctime and variables assigned random numbers. My newCard variable keeps returning the same value each time I call it. Any feedback would be appreciated!

This program is review of loops and cannot include user defined functions

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

int main()
{
    srand(static_cast<unsigned>(time(0)));

    int total = 0;
    int card1 = rand() % 10 + 1;
    int newCard = rand() % 10 +1;
    char deal, replay;

    do
    {   
         cout << " First Cards: " << card1 << ", " << newCard;
         total = card1 + newCard;
         cout << "\n Total: " << total;
         cout << "\n Do you want another card? (Y/N) ";
         cin >> deal;

         while(deal == 'y' || deal == 'Y')
         {
             cout << "\n New Card = " << newCard;
             total += newCard;
             cout << "\n Total: " << total;

            if(total == 21)
            {
                cout << "\n Congratulations!! BLACKJACK! ";
                cout << "\n Would you like to play again? (Y/N):";
                cin >> replay;
                break;
            }
            else if(total > 21)
            {
                cout << "\n BUST ";
                cout << "\n Would you like to play again? (Y/N):";
                cin >> replay;
                break;
            }

            cout << "\n Would you like another card? (Y/N): ";
            cin >> deal;
         }

         while (deal == 'n' || deal == 'N')
         {
             cout << "\n Would you like to play again? (Y/N): ";
             cin >> replay;
         }
    }
    while(replay == 'y' || replay == 'Y');

    while (replay =='n' || replay == 'N')
    {
        cout << "\n Exiting BlackJack \n\n";
        return 0;
    }
}
Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
Lilly B
  • 11
  • 1
  • 2
  • 1
    A variable holds the same value unless you change the value it holds. That's how variables work... – user253751 Sep 03 '18 at 01:24
  • 3
    Analogy: Yesterday I rolled a pair of dice and got an 8. I wrote 8 on a bit of paper. How come the bit of paper says 8 every time I look at it? – user253751 Sep 03 '18 at 01:25
  • Like I said...I'm a beginner and have no experience with C++ so actual feedback would be helpful. I was looking for information about how the random number generator works. I'm unsure of how to go about changing the value to be another random number (I thought declaring srand did this). – Lilly B Sep 03 '18 at 01:35
  • You did that part (the seeding) fine. Your problem is more of a fundamental one that has nothing to do with the random number. `int newCard = rand() % 10 +1;` executes 1 single time when you run your program. The value of `newCard` does not change. `c++` does not reevaluate a formula if you thought that would happen. – drescherjm Sep 03 '18 at 01:36

3 Answers3

1

If you want to generate a random number you need to call rand().

So here:

int newCard = rand() % 10 +1;

I roll a 10-sided dice, it comes up 5, so I write 5 on a piece of paper labeled newCard.

Now, every time I look at my piece of paper labeled newCard, it's going to still say 5. It doesn't change every time I look at it.

If you want to roll again, you need to roll again and write down the new number, by running this again:

newCard = rand() % 10 +1;
user253751
  • 57,427
  • 7
  • 48
  • 90
0
int card1 = rand() % 10 + 1;
int newCard = rand() % 10 +1;

You deal the first two cards correctly(a) but, at no point after that do you actually deal a subsequent card. Instead, each supposedly new card that you check will simply be a duplicate of the second one dealt.

What you need to do is to actually generate a new card if the player is hitting.

This is as simple as inserting a line into your loop:

 while(deal == 'y' || deal == 'Y')
 {
     newCard = rand() % 10 +1;              // <<<--- this one.
     cout << "\n New Card = " << newCard;
     :
}

(a) Not quite correctly for Blackjack since there are more ten-value cards than regular cards (10/J/Q/K all have a value of ten).

A better way would be to use something like:

newCard = rand() % 13 + 1;
if (newCard > 10) newCard = 10;
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
0

Your problem

You only call rand() once at the beginning like this

int card1 = rand() % 10 + 1;
int newCard = rand() % 10 +1;

However, everytime you want a new value, you need to reasign newCard like this

newCard = rand() % 10 +1;

before using it again. You have to call it over and over to get new values.

A Hint

Just because it has not been mentioned yet, here is a good talk about why you should not use std::rand(). While the other answers will also yield random numbers, their distribution may be off. C++ provides a better way to obtain random numbers and I suggest you get used to it as soon as possible.

std::random_device rd;  //Will be used to obtain a seed for the random number engine
std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd()
std::uniform_int_distribution<> dis(1, 10);

int newCard = dis(mt);

You need the following headers for that

#include <random>
#include <iostream>

If you need even better randomness you can use random_device rather than the mt19937 generator.

So I advice to do it right from the start and remove rand() from your code.

  • I appreciate that advice! Unfortunately the introduction class I am in has not gotten this far and I am not supposed to surpass his teaching. I will definitely put this to practice on my own time though!! Thank you! – Lilly B Sep 05 '18 at 01:35