Roulette Program Help - randomness, infinite loop, etc.

Hi, erm, new here. I'm writing a Roulette randomness program that simulates...well, the roulette game. However, after the "switch (play_method)" chunk, it keeps looping infinitely and skipping the "switch (color)" part.

I've only started on the first case, since I figured that once i got this down everything else would be copy/pasting and tweaking.
If someone could tell me if my use of randomness is correct too, that'd be great...or if there's a better way of shortening this with if statements and loops - at this rate my lines will be in the thousands...
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
#include <iostream.h>
#include <stdlib.h>
#include <time.h>

main()
{
      //declare integers
      int total_amt, bet_amt, win_amt, play_method, r, choice, color, num;
      //may need constants here 
      
      //opening statement
      cout << "Welcome welcome, to Ringside Roulette!" << endl;
      cout << "How much money do you have? $";
      cin >> total_amt;
      cout << "How much money would you like to bet? $";
      cin >> bet_amt; 
      
      //start of overall do while loop
      do
      {
              //choose method of play 
              cout << "1 - Play a color.\n";  
              cout << "2 - Play a number.\n";
              cout << "Please select an option. ";
              cin >> play_method; 
              
              switch (play_method)
              {
                     case 1:    // color play 
                     cout << "A - Play Red.\n";
                     cout << "B - Play Black.\n";
                     cout << "C - Play Green. (Hit the leprechaun's jackpot!)\n"; 
                     cout << "Which would you like to bet on? " << endl;
                     cin >> color;
              
                        switch (color) 
                          {
                                 case 'A':  //the red color
                                 for (r=1; r<0; r=r+2)  
                                 {
                                     r = rand()%18+1;
                                     cout << "You spun a  " << r << endl;
                                     if (color == r)
                                     {
                                               win_amt = bet_amt*2; 
                                               total_amt = total_amt + (win_amt - bet_amt); 
                                               cout << "Lucky! You have won   $" << win_amt << "!" << endl; 
                                               cout << "You now have  $" << total_amt << " remaining." << endl;        
                                     }
                                     else 
                                     {
                                          total_amt = total_amt - bet_amt; 
                                          cout << "Tough luck. You have lost  $" << bet_amt << endl;
                                          cout << "You now have  $" << total_amt << " remaining." << endl;
                                        
                                     } //else end
                                 }  // for end 
                                 
                                 break;  // break case 1 
                          } // end of color switch statment  
                         
              break; 
              } //end of play_method switch statement
              
     cout << "Would you still like to play? Press 1 to continue playing. Press 0 to quit. " << endl;
     cin >> choice; 
}
while (choice!=0); // end of complete do while 
          
return 0;
}


You declare color as an int.

You input a char.

The >> operation fails, cin is in fail state and color remains its original uninitialised value.

color doesn't match any case so program jumps to asking for choice.

Since cin is in fail state, cin>>choice doesn't work.

choice remains original unitialised value which is !=0 and so loop continues.

Since cin remains in fail state values for playmethod and color and choice do not get changed so loop continues forever.
Last edited on
r = rand()%18 + 1 will give you a number between 1 and 18.

For a roulette table, you may want to roll
r = rand()%38; as there are typically 38 numbers on a roulette table. This would give you a range of 0 to 37. You could tread 0 as 0 and 37 as 00.

One thing that you should do, is at the top of your main, call srand() and seed it with something such as the time.
Thank you very much, both of you! This helped a lot!

I'm a little confused though, Stewbond - what is srand? Does it have to be in the code for it to be able to generate random numbers, because my teacher didn't put it in and he was fine. Also, i used the equation that you gave me, but i keep getting "4" whenever i try generating a number - is it a glitch with the compiler or something to do with having to put in srand? (I'm trying to put up my code on here but the limiter says its too long...)
Are you still coding this?
Here is a Spin() method from my roulette project that produces radnom numbers between 0 and 36.


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
         #include <random>		// used in Spin function

 	void CSession::Spin(short number)
	{
		using std::mt19937_64;
		using std::seed_seq;
		using sets::VoisinsDeZero;   // it is array of numbers
		using std::uniform_int_distribution;

		if (number > 0)
		{
                        // mpTable is pointer to roulette table object
			mBankrol += mpTable->SetOutcome(number);
			return;
		}

		static bool seed = true;
		static std::shuffle_order_engine<mt19937_64, 255> mGenerator;
		static seed_seq seeder(VoisinsDeZero, VoisinsDeZero + 17);
		static uniform_int_distribution<short> mDistribution(0, mNums - 1);

		if (seed)
		{
			
			mGenerator.seed(seeder);
			seed = false;
		}
                 // mpHistory is pointer to the stack of all numbers comed out
		mpHistory->push(mDistribution(mGenerator));
		mBankrol += mpTable->SetOutcome(mpHistory->top());
	}



Maybe this give you an general idea :)
Topic archived. No new replies allowed.