blackjack game

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

thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include<iostream>
#include <cstdlib>  //to use rand function
#include <ctime>  // to use time as the seed for rand
using namespace 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;
}
A simple loop would work:

1
2
3
4
5
while (handValue < 21)
    {
        //would you like another? 
        //blah blah blah
    }



If you could be more specific where you are having trouble (in your source) that would be great.
Topic archived. No new replies allowed.