I'm trying to write a blackjack game, but I ran into a problem with random integers. I'm to the point where the player has there original number, and they've chosen to hit. I need help figuring out how to make random_integer keep it's value, but then add another random integer to it. what I have now is just random_integer + random_integer, but that just doubles the original value of the original random_integer. So really what I'm asking is how to get two different random integers. I'll copy&paste what I have. Note that I'm jumping around a lot in this, so if you see errors elsewhere it's probably just because I haven't gotten to them yet lol.

#include <cstdlib>
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int number;
char ans;
char Ans;


    
    
int main(int argc, char *argv[])
{
    do
    {
             
    cout << " Welcome to Blackjack\n";
    cout <<"\n";
    system("PAUSE");
    cout << "\n";
    #
srand((unsigned)time(0));   // generates random number.
int random_integer = rand();
int lowest=1, highest=12;
int range=(highest-lowest)+1;
for(int index=0; index<1; index++){
random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0));
cout << "You've been dealt a(n) " << random_integer << endl;
cout << "\n";
}
if (random_integer==21) {
                        do 
                        {
                        cout << "BACKJACK!!! \n";
                        cout << "\n";
                        cout << "Do you want to play agian? (y/n)\n";
                        cout <<"\n";
                        cin >> ans;
                        }
                        while((ans !='y')&&(ans !='n'));
                        if (ans=='y')
                           system("cls");
                           cout << "okay";
                        }
                       else if (ans=='n') 
                             cout << "Goodbye!";
                             
                             
                        
 if (random_integer<21) {
          cout << "to hit, press 1. to stay, press 2.\n";
          cout << "\n";
          cin >> Ans;
                 if(Ans=='1')
                             cout << "Now you're at " << random_integer + random_integer << ".";

<---------- this is what I'm talking about.

Recommended Answers

All 4 Replies

Also note that I am VERY new to c++ lol. If you couldn't tell already. So if you have any advice, please feel free.

You should make a variable called playerHand or something that will keep track of what the players current hand is worth. That way you can just add the random integer to it and then make a new random integer while still keeping the value of the players hand.

The very first game I ever made was blackjack and I made a deck of 52 cards and used rand() % 52 to pick a card out of the deck (array[52]) and just added the value of the card to the players hand. If you want I can attach the source code for that even tho I haven't looked at it for a really long time so some stuff could be poorly written.

hmm.. for now I'll just look into arrays and then if I still can't figure it out then I'll send you a message lol. If that's alright with you?

Yeah sure.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.