Roulette Program Problem

Hello everyone, trying to generate a game of roulette. But I think Im going about it the wrong way. It produces a funky output and Im not sure what Im doing wrong or out of place. Granted I've been up for a long time and its late at night. If you see anything Im missing please poke me in the eye with it.
CODE:
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
  #include <iostream>
#include <cmath>
#include <stdlib.h>
using namespace std;

int main()
{
	char op,e;
	int x,n,t,zero,bet,result,wins,losses;
	int start = 100;
	bet = 5;
	zero = 50;
	n = start + bet;
	t = start - bet;

	cout<<"Welcome to The Roulette Wheel, you can bet on Even, Odd, or Zero\n";
	cout<<"Use the letters E,O or Z, each bet is 5 dollars\n";
	cout<<"You can choose Q to quit game,the game ends automatically when the moneys gone\n\n\n\n";

	do
	{
		cout<<"you have "<<start<<endl;
		cout<<"please select E,O,Z,or,Q\n";
		cin>>op;
		x= rand() % 10;

		switch(op)
		{
case 'E':
    {
	for(int i=1;i<=1;i++)
	{
	cout<<x<<endl;
	if(x % 20 == 0)
	{
	cout<<"You win!!!! and your new balance is "<<n<<endl;
	start += bet;
	}
	else
	{
	cout<<"You lose and your new balance is "<<t<<endl;
	start -= bet;
	}
	break;

	}
	case 'O':
	{
	for(int i=1;i<=1;i++)
	{
	cout<<x<<endl;
	if(x <= 20 == 0)
	{
	cout<<"You win!!!! and your new balance is "<<n<<endl;
	start += bet;
	}
	else
	{
	cout<<"You lose and your new balance is "<<t<<endl;
	start -= bet;
	}
	break;
    }
    case 'Z':
	{
	for(int i=1;i<=1;i++)
	{
	cout<<x<<endl;
	if(x >= 20 == 0)
	{
	cout<<"You win!!!!! and your new balance is "<<n<<endl;
	start += bet;
	}
	else
	{
	cout<<"You lose and your new balance is "<<t<<endl;
	start -= bet;
	}
	break;
	}
	}
	}
	cout<<"Do you want to continue? <Y or N> \n";
	cin>>e;
	}
	}
	}
	while((e ==  'y')|| (e == 'Y'));}



Thank you for any help.
#1: You should seed the random number generator with strand (time(0)), otherwise you program just comes up with the same results every time.

#2: What do you think happens here for(int i=1;i<=1;i++). If it wasn't for the break statement, your code would go on forever.

#3: Number generator should be x = rand () % 37. This will yield values between 0 and 36.

#4: Format you code so it's easier to read
1
2
3
4
5
6
7
8
9
10
11
12
case 'E':
	cout << x << endl;
	
	if(x % 20 == 0) {
		cout<<"You win!!!! and your new balance is "<<n<<endl;
		start += bet;
	}
	else {
		cout<<"You lose and your new balance is "<<t<<endl;
		start -= bet;
	}	
break;

Notice I took the for/next loop out. Why was it there?

#5: What is the reasoning for this if(x <= 20 == 0).

#6: Beside (E)ven (O)odd (Z)ero, you should have (T)op 1-12 (C)enter 13 - 24 (B) 25 - 36, (L)eft 1-4-7 etc (M)idle 2-5-8 etc (R)ight 3-6-9 etc. There are a lot of other combinations, but at least these would make it a little more realistic.

#7: Asking to continue and having a selection of "Q" is redundant.

I think I'm going to undertake designing this application. It presents a lot of opportunity for interesting logic.
Ok first Im going to apologize for my ignorance. I was trying to follow a example in my book that is similar but it didnt come out quite right. And answers to your questions.

1# I have no clue where to place this in my code strand (time(0)) for proper seeding.

2# for(int i=1;i<=1;i++) I honestly have no clue why this was in there except it was in the example and the example worked with it so I left it in.

3# I modified my generator or atleast where this looks like it goes in the code.

4# Formating; This is just a rough draft to get thoughts on the screen. I usually indent and space properly after I get what I need on the screen.

5# if(x <= 20 == 0). I had no reason for this except trying to make something work that didnt give me a weird output, no output or wrong output. I was running out of ideas and my book is like ready the bible in hebrew translated from japanese. Not very good of a book my teach gave us.

6# I changed this to the recommended t, c, b but dont know what you mean by the numbering?

7# Asking to continue with a Q was the only way I could find to get multiple plays in this game. But now that I pulled it out I cant think or find a way to make it play multiple times.

CODE: This is what I've modified; But it still comes out wrong. Damn Im stupid, Chuckle.
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
 
#include <iostream>
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;

int main()
{
	char op,e;
	int x,n,t,zero,bet,result,wins,losses;
	int start = 100;
	bet = 5;
	zero = 50;
	n = start + bet;
	t = start - bet;

	cout<<"Welcome to The Roulette Wheel, you can bet on Even, Odd, or Zero\n";
	cout<<"Use the letters (T)op, (C)enter and (B)ottom, each bet is 5 dollars\n";

	do
	{
    cout<<"You have "<<start<<endl;
	cout<<"Please select T, C, or B";
	cin>>op;
	x = rand () % 37;


    switch(op)
	{
    case 'T':
        {
        cout<<x<<endl;

        if(x % 20 == 0)
        {
		cout<<"You win!!!! and your new balance is "<<n<<endl;
        start += bet;
        }
        else
		cout<<"You lose and your new balance is "<<t<<endl;
        start -= bet;
        }
        break;

        case 'C':
        {
        cout<<x<<endl;

        if(x % 20 == 0)
        {
        cout<<"You win!!!! and your new balance is "<<n<<endl;
        start += bet;
        }
        else
        {
        cout<<"You lose and your new balance is "<<t<<endl;
        start -= bet;
        }
        break;

        case 'B'
        :{
        cout<<x<<endl;

        if(x >= 20 == 0)
        {
	    cout<<"You win!!!!! and your new balance is "<<n<<endl;
        start += bet;
        }
        else
        {
        cout<<"You lose and your new balance is "<<t<<endl;
        start -= bet;
        }
        break;
        }}}}
        while (0);
}






FYI; Sorry Im just trying to follow the book and its examples and its really really confusing. Plus the teach aint much help either because he's not a programmer. He's actually a math instructor.
Last edited on
srand only needs be executed once. The examples I'm giving are only to demonstrate a concept and can't be compiled.

1
2
3
4
5
6
7
8
9
10
11
12
int main (void) {
	
	srand (time(NULL));
	
	do {
		int Roll = rand () % 37;		// Generate numbers between 0 & 36.
		// Code here to place bets
		//           to determine wins
	} while ( choice != 'Q' );
	
	return 0;
}

#2 is not a method I would use although most often you'll see
1
2
do {
    } while (1);
or even while (true). That is the proper way to do an infinite loop when you need one, but that for/next inside a case is totally meaningless.

Try to avoid redundancy and in your case because you want to display "x" each time, put it outside switch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
do {
	int Roll = rand () % 37;		// Generate numbers between 0 & 36.
	cout << x << endl;
	cin >> choice;
	toupper (choice);
	
	switch (choice) {
		case 'O':
			break;
		case 'E':
			break;
		case 'T':
			break
		case 'Q'
			break;
		default:
			cout << "Not a valid choice" << endl;
	}
} while ( choice != 'Q' );
Formating; This is just a rough draft to get thoughts on the screen. I usually indent and space properly after I get what I need on the screen.
Sounds logical, but trust me, when your apps get more complex, mismatched braces become a big problem and not being able to see program flow easily is another, it only takes seconds to hit TAB and if you use an editor like NotePad++ it's even easier.

That conditional probably should look something like this if (x <20 && x >=0) is probably what it should be.
I changed this to the recommended t, c, b but dont know what you mean by the numbering?
Don't worry about that, just get your original ones like O, E, Z & Q working first, then you'll see how to implement others.

I'm really curious as to how somebody that isn't a programmer can teach you programming and if in fact you are copying out of a book, verbatim without any mistakes, throw that book away.
I agree with you on the formatting. I can see where larger code would become very difficult without proper indenting and spacing. And I couldnt agree with you more about the teaching thing. He's a math instructor with a degree in mathematics but only has a passing fancy with programming so he got roped into the class because no one else wanted it. Atleast thats the story Im getting from it. I mean we have three different programming styles in one class. C#, C++ and Java. Makes for a interesting class period.

And as for the book. I dont think its that bad I just think its that confusing to someone who has never done much programming. Its not exactly a level 101 book its more like a level 300 book. The book doesnt do a good job of describing the various parts of the code. They just give you a examples then a "small" paragraph on what the example is supposed to do.

But thank you for the advice. I was able to make it work to and extent. It actually didnt start working tell a few extra compilings but whatever. Thank you.
Topic archived. No new replies allowed.