While Loop Problem

closed account (EvoTURfi)
Hello,
I'm trying to code a simulation of a card game called Acey Ducey where the computer outputs two cards. Then the user chooses to bet money or not. Then, the computer outputs a third card. If the third card is between the first two cards then no money is lost for the user. Otherwise, the amount they bet is lost. I'm running the code, but it doesn't seem to work. When compiled the code keeps looping over and over again. The loops in my code have breaks, but the program seems to skip them! Please 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
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
#include<iostream>
#include<time.h>
using namespace std;

int main()
{
	int money=100;
	int card1,card2,card3;
	char valid1[2]="n";
	char valid2[2]="o";
	int bet=0;
	while(money!=0)
	{
		srand(time(0));
		card1=rand() %13 +1;
		card2=rand() %13 +1;
		card3=rand() %13 +1;
		while(valid1[2]=='n')
		{
			if(card1==card2 || card1==card3)	// Check if cards are the same
			{
				srand(time(0));
				card1=rand() %13 +1;
				break;
			}
			else if(card2==card1 || card2==card3)
			{
				srand(time(0));
				card2=rand() %13 +1;
				break;
			}
			else
				break;
		}
		cout<<"Your cards are:"<<endl;	// Display cards
		if(card1==11)
		{
			cout<<"Jack"<<"\t"<<card2;	// Checks for jacks, queens, or kings
		}
		else if(card2==11)
		{
			cout<<"Jack"<<"\t"<<card1;
		}
		else if(card1==12)
		{
			cout<<"Queen"<<"\t"<<card2;
		}
		else if(card2==12)
		{
			cout<<"Queen"<<"\t"<<card1;
		}
		else if(card1==13)
		{
			cout<<"King"<<"\t"<<card2;
		}
		else if(card2==13)
		{
			cout<<"King"<<"\t"<<card1;
		}
		else
		{
			cout<<card1<<"\t"<<card2<<endl;
		}
		while(valid2[2]=='o')
		{
			cout<<"How much do you want to bet?"<<endl;	// Asks for bet amount
			cin>>bet;
			if(bet>100)
			{
				cout<<"Bet is greater than $100!"<<endl<<endl;	// Checks if bet is valid
				continue;
			}
			else if(bet<0)
			{
				cout<<"Bet is less than $0!"<<endl<<endl;
				continue;
			}
			else if(bet==0)
			{
				cout<<"CHICKEN!!!"<<endl;
				break;
			}
			else
			{
				break;
			}
		}
		if(card3>card1 && card3<card2)
		{
			cout<<"The next card, "<<card3<<", is between card 1 and card 2!"<<endl;	// Outputs next card and how much money was lost if any
			cout<<"You lost no money!"<<endl;
		}
		else if(card3>card2 && card3<card1)
		{
			cout<<"The next card, "<<card3<<", is between card 1 and card 2!"<<endl;
			cout<<"You lost no money!"<<endl;
		}
		else
		{
			money-=bet;
			cout<<"Sorry but the next card isn't between the previous two cards!"<<endl;
			cout<<"You lost "<<bet<<" dollars, leaving your total money as "<<money<<endl;	// Loops to the beginning again
		}
	}
	cout<<"I'm sorry but you lost all your money! Better luck next time!"<<endl;	// Displays when money is 0
	return 0;
}
Last edited on
dvader123 wrote:
I'm running the code, but it doesn't seem to work.
That's a very descriptive problem, let me get right on that... First of all I don't understand the premise of the game. Why would I play when the only two outcomes are to lose money and not lose money. How do you win? That aside I do see a few WTFs:
1
2
3
4
5
6
7
8
9
10
char valid1[2]="n"; //line 9
char valid2[2]="o";
//Why not just make these char's?

//Like this:
char valid1 = 'n',
     valid2 = 'o';

//Or, if you want to use an array, like this:
char valid[2] = { 'n', 'o' };

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
while( ... ) //line 18
{
	...
	srand(time(0)); //line 22
	...
	srand(time(0)); //line 28
	...
}
//You are srand-ing inside a loop. You only need to seed the random number generator once!

//Just call it once before the loop starts. Like this:
srand( time(NULL) );
while( ... )
{
	...
}

//That being said, in every case, your while-loop breaks. Why not just use an if-statement? 

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
if(card1==11) //line 36
{
	cout<<"Jack"<<"\t"<<card2;
}
else if(card2==11)
{
	cout<<"Jack"<<"\t"<<card1;
}
...
else //line 60
{
	cout<<card1<<"\t"<<card2<<endl;
}
//This long else-if chain could most likely be replaced by a switch-statement.
//But first, it doesn't make sense that (card2==11) should depend on the previous (card1==11),
// so why is this an else-if at all?

//You most likely wanted something like:
switch(card1) {
 case 11:
	cout << "Jack";
	break;
 case 12:
	cout << "Queen";
	break;
 case 13:
	cout << "King";
	break;
 case 1: //You forgot this case in your code...
	cout << "Ace";
	break;
 default:
	cout << card1;
}
cout << "\t";
switch(card2) {
	... //same as above
	    // (hint, hint... you could make this a function, or better yet, make card a class! ;)
}
cout << endl;

//There's an even faster yet messier way:
cout << (card1==1 ? "Ace" : card1==11 ? "Jack" : card1==12 ? "Queen" : card1==13 ? "King" : card1) << "\t";
cout << (card2==1 ? "Ace" : card2==11 ? "Jack" : card2==12 ? "Queen" : card2==13 ? "King" : card2) << endl;
closed account (EvoTURfi)
Thank you VERY MUCH!!! I thought this was a compiler problem. Never realized that a srand() inside a loop will cause this many problems.
It's not a syntax error to put srand in a loop, so your compiler should not balk. It will just cause your rand calls to be not pseudo-random.
Last edited on
Topic archived. No new replies allowed.