hello
i am working on a blackjack game,it asks randomly generates numbers, then user has to input them. the program then calculates the total. if it is less than 21 it asks if you want a new card. if 21 it says you win. if more than 21 you bust.
problem is i dont know how to make it stop once it gets over 21, my program also will draw another card after 21.
also any hints as to make the program easier, like a loop or something. at first i was thinking a loop, but i dont know how to cin all the different variables with one loop.
ps i dont care about the indentation, looks better on visual studio
#include<iostream>
#include <cstdlib> //to use rand function
#include <ctime> // to use time as the seed for rand
usingnamespace std;
int main(){
int guess, number,numcards, cardnum;
int cardnumber;
int n1,n2,n3,n4,n5;
int numberGuesses;
int total;
char morecard,playagain;
srand(time(0));
/* while (numberGuesses < 10)
{
number = 1 + rand() % 10;
cout << number << endl;
numberGuesses++;
}
*/
for (int n=2; n>0; n--) //outputs random numbers for cards
{
numberGuesses = 1;
number = 1 + rand() % 10;
cout << number<<endl;
}
cout<<"how man cards do you have? \n"; //asks how many cards you got
cin>>cardnum;
cout<<"what is the card value? \n"; //asks what the number of the card was
cin>>n1>>n2; //stores card value
total=n1+n2; //computes total
cout<<"Your total is "<<total<<". \n";
if(total<=20) //if not 21
{
cout<<"do you want to get another card <Y or N>?";
cin>>morecard;
}
if(total<=20 && morecard=='Y' || morecard=='y') //if you want another card
{
numberGuesses = 1;
number = 1 + rand() % 10;
cout <<"3rd card: "<< number<<endl;
cout<<"What was the value of the card? ";
cin>>n3;
}
total=n1+n2+n3; //computes total for 3 cards
cout<<"Your new total is "<<total<<" \n";
if(total<=20) //if not 21
{
cout<<"do you want to get another card <Y or N>?";
cin>>morecard;
}
if(total<=20 && morecard=='Y' || morecard=='y')
{
numberGuesses = 1;
number = 1 + rand() % 10;
cout <<"4th card: "<< number<<endl;
cout<<"What was the value of the card? ";
cin>>n4;
}
if(total==21) //if 21
{
cout<<"You win!!!";
}
if(total>=22) //if over 21
{
cout<<"BUSTED";
}
total=n1+n2+n3+n4;
cout<<"your new total is "<<total<<" \n";
if(total<=20) //if not 21(4cards)
{
cout<<"do you want to get another card <Y or N>?";
cin>>morecard;
}
if(total<=20 && morecard=='Y' || morecard=='y')
{
numberGuesses = 1;
number = 1 + rand() % 10;
cout <<"5th card: "<<number<<endl;
cout<<"What was the value of the card? ";
cin>>n5;
}
total=n1+n2+n3+n4+n5;
cout<<"your new total is "<<total<<" \n";
if(total==21) //if 21
{
cout<<"You win!!!";
}
if(total>=22) //if over 21
{
cout<<"BUSTED";
}
else
{
cout<<"you lose \n";
}
cout<<"Play again <Y or N> "<<endl;
cin>>playagain;
system("pause");
return 0;
}