Craps Game Malfunctioning

Whenever I try to run this Craps game I recieve an error. It gives me an error message saying, '}' needed before line 157. Any helpful suggestions about how this program might work better is greatly appreciated. Thank you in advance...
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
139
140
141
142
143
144
145
146
147
148
149
#include <iostream>
#include<cstdlib>
#include<iomanip>
#include "rolldice.cpp"


//******************************************************************************

using namespace std;
       
   int main()

{  
  

  
    const int LUCKY_SEVEN = 7,
           CRAPS = 7,
           YO_LEVEN = 11,
           SNAKE_EYES = 2,
           TREY = 3,
           BOX_CARS = 12;
           
   
   const double MINIMUM = 500.00;
  

           
   int die1, die2, point, dieSum;
   double purse, bet;
   bool won, quit;
   char response;
purse=1000;
void rolldice(int& die1, int& die2);  
   


   cout<<"This program will simulate the game of craps and will allow you to bet\n"
       <<"a certain amount of money. Based on your roll, you will win or lose money.\n"
       <<"If on your first roll, you roll a 7 or an 11, you will win (Craps Out!)\n"
       <<"Until you Crap Out, you will continue to roll. Your starting amount of\n"
       <<"money is $1000. You will win or loose money based upon what you have\n"
       <<"wagered. Good luck!.\n\n";

   
cout<<"You have "<< purse <<" dollars, you will be asked to place a bet until\n"
    <<"you run out.\n";

if (purse<MINIMUM)
{
cout<<"You must exit the game so that you can restart the value in your purse.\n";
}


do
{
cout<<"You have"<< purse <<"dollars to start with.\n";
do
{
cout<<"Please enter a valid bet, bet must be greater or equal to $500.\n";
cin>>bet;
do
{
cout<<"You have bet "<< bet <<"dollars. Let us roll now...\n";

}while(bet <500 || bet!=0);

cout<<"Roll the dice!\n";
system("PAUSE");
rolldice(die1,die2);
dieSum = die1 + die2;

cout<<"You rolled a "<<die1 + die2<<".\n";  
    
switch (dieSum)    
 {
    
         case LUCKY_SEVEN:      
         case YO_LEVEN:    won = true;                             
         break;
            
    
         case SNAKE_EYES:                 
         case TREY:                    
         case BOX_CARS:   won = false ;
                          
                          break;     
         default: point = dieSum;
           
cout<<"Your roll, "<<die1 + die2 <<"becomes your point.\n";
            do
              {
              cout<<"Roll the dice!\n";
system("PAUSE");
rolldice(die1,die2);
dieSum = die1 + die2;
}while(dieSum != CRAPS);
switch (dieSum)    
 {
    
         case LUCKY_SEVEN:      
         case YO_LEVEN:    won = false;                             
         break;
            
    
         case SNAKE_EYES:                 
         case TREY:                    
         case BOX_CARS:   won = true ;
                          
                          break;     
         default: point = dieSum;
         
 // set won to true or false depending on outcome of dieSum


if(won)
{
cout<<"Congratulations, you have won. Your purse is now "<<purse + bet <<"!\n";
}
else
{
cout<<"Unfortunately you have lost, your purse is now "<< purse - bet <<"...\n";
}
if(purse!=0)
{
cout<<"You have "<< purse <<"!Cool...\n";
}
do
{
cout<<"Would you like to play again?\n";
cin>>response;
response = toupper(response);
if(response != 'Y' && response != 'N');
cout<<"Illegal response, please answer Y for yes or N for No:\n";
}while(response != 'Y' && response != 'N');
if(response=='N')
{
cout<<"Ok then...\n";
}
else( purse == 0 )
{

cout<<"Please exit and reenter the game to restart the amount you have in your purse.\n";

}while(purse!=0);
cout<<"Please come back soon.\n";
system("PAUSE");
return 0;
}
Line 144 needs a brace }
Yeah...unfortunately after I put a bracket there it still reads as an error... Does my code for the game look correct.
Post the "rolldice.cpp" code. I have never played Craps so I don't know how the game works but I can find the error and try to fix it.

On line 34, I am assuming that it is a function call. You have it structured like a prototype. If it is a function call you need to remove the void
If it is a prototype then you need to move it outside of the main function.
Last edited on
Here is rolldice.cpp
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
************************************************************************/
#include "random.cpp"
#include<iostream>
using namespace std;

void rolldice(int& die1, int& die2)
{
   int random(double seed);
   
   double seed1, seed2;
   do
   {
     cout<<"\n\nEnter two seed values between 0 and 1, separated by a space,\n";
     cout<<"and then press ENTER to simulate the rolling of a pair of dice.\n";
     cin>>seed1>>seed2;
     if(seed1<0 || seed1>1 || seed2<0 ||seed2>1)
       cout<<"Seed values must be between 0 and 1!\n";
   }while(seed1<0 || seed1>1 || seed2<0 ||seed2>1);
   
   die1 = random(seed1);
   die2 = random(seed2);
   return;
}

   
I have never played craps as well so understanding what code to put in and adjusting the rules sucks. I think rolldice.cpp also needs random.cpp. So here is random.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
This file consists of a pseudo random number generator that
returns a "random" number between 1 and 6 when it receives 
a seed value between 0 and 1.

********************************************************************/

#include<cmath>

int random(double seed)
{
    seed = static_cast<double>(pow(seed+3.14,2));
    seed = seed - static_cast<int>(seed);
    return 1 + static_cast<int>(6 * seed);
}

Thank you for the help...
Between lines 63 and 66 This code could would go into an infinite loop if the user enter something other than 0.

I would have one main while loop and forget about all of those do-while loops. Try to make it use only one main game loop and have all your other loops small so the flow of control is clear.
What might this look like, if you don't mind illustrating it. Most of the time I have a difficult time making such programs.
I am not going to take the time to study Craps but here would be your basic template.

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
#include // all your includes

using namespace std;

int main()
{
  char choice = 'Y';

  //Initialize all your other variables

  do
  {
  // start the game
  // roll the dice and do whatever

  // You probably only need one do while or while loop here so there can
  // be multiple rolls

  // end of loop
  cout << "Would you like to play again? (y/n) ";
  cin >> choice;
  }while( (choice == 'Y') || (choice == 'y') );

  // close down the program
 return 0;
} 


Btw your random number generator is much more complex that it needs to be.
Why not use?
random_number = rand() % 6 + 1;
Last edited on
definately follow eker676's advice on the random number generator, just remember you have to seed the generator once and only once in your program before you call rand(). You can remove all that seed junk you have in your random function, heck, that whole function isn't needed, just more chances to make mistakes.
Just use the time() function at the start of main
 
srand(time(0));

This provides the only seed you need.
then use:
1
2
die1= rand() % 6 + 1;
die2= rand() % 6 + 1;

whenever you want to get a die roll. Your rolldice function only needs those two lines. You can eliminate a big chunk of your program by using these 3 simple lines
Last edited on
Topic archived. No new replies allowed.