BlackJack bool loop

So in my Computer Science class we had to write a basic game, so i wrote a simple blackjack program. My only issue is my teacher dislikes the use of "goto", and suggested bool loops. Can anyone help me with this? i have no idea what to do with bool loops or how to make one, and a google search offered no help
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
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace::std;

int main()
{
int card1, card2, cardSum, compSum, Move;
compSum = 10;
srand(time(0));

card1 = (rand() %11) + 1;

card2 = (rand() %10) + 1;

compSum += (rand() %11) + 1;

cardSum = (card1 + card2);

label:
cout << "Your cards total " << cardSum << endl;
cout << "Would you like to Hit or Stay?" << endl << "(1) Hit" << endl << "(2) Stay" << endl;
cin >> Move;
cout << endl;

if (Move == 1)
	{
	srand(time(0));
	cardSum += (rand() %10) + 1;
	goto label;
	}
if (Move == 2)
	{
	cout << "Your total is " << cardSum << endl;
	cout << endl << "The Computers total is " << compSum << endl;
	
	if (compSum > cardSum)
		{
		cout << endl << "You lost to the computer." << endl << endl;
		}
	if (cardSum > 21)
		{
		cout << endl << "You were over 21 and busted, you lost." << endl <<endl;
		}
	if (cardSum > compSum && cardSum <= 21)
		{
		cout << endl << "You win!" << endl << endl;
		}
	if (cardSum == compSum)
		{
		cout << "Its a draw! play again!" << endl << endl;
		}
	}
return 0;
}
//Odobenus 
Last edited on
You just need:
1
2
3
4
5
6
7
while(move == 1)
{
    //put in all the code for hitting in here; 
}
    //Put in all the code for stay here 
return 0; 
}


If you want to improve this game a little bit, you might want to look at doing:

-A standard deck of cards has 12/52 cards which give a 10, make your code reflect this.
-If you are dealt 11 + 10, that is an instant win, called a blackjack (and you get big time riches at the casino)!
Last edited on
I'm sorry, I'm not sure what you mean. realize i have no idea what a bool loop is or anything. My teacher said "use one of these" then gave me no information on it.

Also for the 12/52, i was thinking of having another random number gen with another variable, probably "chance10". it generates a number between one and 52 and if its 1-12, ten will be added to cardSum. is there a more efficient way to do that or would that work?
I've never heard that term before, but all loops are basically boolean, meaning they run until a condition becomes either true or false.

Random numbers are not the best way to simulate a deck of cards. If you use a vector, all you need to do is call shuffle() then take the cards off the top just like a real deck.
So this is what i ended up with, just changed the "if" too "while" and deleted the second if. And it does still work, but the problem is the "goto" being there. for some unknown reason she doesnt like that there. so how would i make this code work without using goto?

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
#include <iostream.h>
#include <ctime>
#include <cstdlib>

int main()
{
int card1, card2, cardSum, compSum, Move;
compSum = 10;

srand(time(0));
card1 = (rand() %11) + 1;

srand(time(0));
card2 = (rand() %10) + 1;

srand(time(0));
compSum += (rand() %11) + 1;

cardSum = (card1 + card2);

label:
cout << "Your cards total " << cardSum << endl;
cout << "Would you like to Hit or Stay?" << endl << "(1) Hit" << endl << "(2) Stay" << endl;
cin >> Move;
cout << endl;

while (Move == 1)
	{
	srand(time(0));
	cardSum += (rand() %10) + 1;
	//insert pineapple
	goto label;
	}

	{
	cout << "Your total is " << cardSum << endl;//omar was also here
	cout << endl << "The Computers total is " << compSum << endl;
	
	if (compSum > cardSum)
		{
		cout << endl << "You lost to the computer." << endl << endl;
		}
	if (cardSum > 21)
		{
		cout << endl << "You were over 21 and busted, you lost" << endl <<endl;
		}
	if (cardSum > compSum && cardSum <= 21)//omar was here =p
		{
		cout << endl << "You win! C:" << endl << endl;
		}
	if (cardSum == compSum)
		{
		cout << "Its a draw! play again!" << endl << endl;
		}
	}
return 0;
}
There is a very very good reason for your teacher not wanting that goto statement to be there. Putting goto statements in code makes it very quickly unreadable and impossible to modify or maintain. You can get rid of that goto and just put the code asking the user what they want to do inside the while loop.

Everything after goto label; in your while loop can go outside and after the while loop.

Ask yourself this? Does the code need to execute more than once? If the answer is no, it should not be in a while loop.
Mats wrote:
Everything after goto label; in your while loop can go outside and after the while loop.

It IS outside of the while loop, even though his indentation may indicate otherwise.
@OP, take out all those calls to srand(). You only need to do it once. Line 10 is ok, but remove the rest of them.
Also, read about the do-while loop.
Last edited on
Topic archived. No new replies allowed.