The age-old slot machine...

at this point i've scratched a bald spot into my head. any advice/pointers/clues would be hugely appreciated.

here are the errors being generated:

Error 2 error C2065: 'winnings' : undeclared identifier c:\users\dev\documents\visual studio 2012\projects\consoleapplication6\consoleapplication6\consoleapplication6.cpp 199 1 ConsoleApplication6
Error 3 error C2065: 'winnings' : undeclared identifier c:\users\dev\documents\visual studio 2012\projects\consoleapplication6\consoleapplication6\consoleapplication6.cpp 201 1 ConsoleApplication6
4 IntelliSense: identifier "winnings" is undefined c:\Users\dev\Documents\Visual Studio 2012\Projects\ConsoleApplication6\ConsoleApplication6\ConsoleApplication6.cpp 199 7 ConsoleApplication6




----------------and here is my code----------
---------(edited for formatting...sorry about that)-------
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212

#include <ctime>
#include <iostream>
#include <sstream>
#include <string.h>
#include <random>

using namespace std;




class TripleString
{

   string string1, string2, string3;

public:
   static const int MAX_LEN=20;
   TripleString();

   bool validString( string str );
   void set(string str1, string str2, string str3);
   string get(int strPosition);
};

TripleString::TripleString()
{
string1="";
string2="";
string3="";
}


bool TripleString::validString(string str)
{
   if (str.length()<=MAX_LEN)
      return true;

   return false;
}

void TripleString::set(string str1, string str2, string str3)
{
   if(validString(str1))
   {
      string1=str1;
   }
   if(validString(str2))
   {
      string2=str2;
   }
   if(validString(str3))
   {
   string3=str3;
   }
}

string TripleString::get(int pos)
{
  if(pos==1)
  {
     return string1;
  }
  if(pos==2)
  {
     return string2;
  }
  if(pos==3)
  {
     return string3;
  }
}


int getBet();

TripleString pull();

string randString();

int getPayMultiplier (TripleString thePull);

void display (TripleString thePull, int winnings );


int main()
   {
   srand( time( NULL ) );//seed for rand method   
   TripleString thePull;
   int bet, mult, winnings;
   //string mult, bet;
  
  
   bet = getBet(); 
   //stringstream convert(bet);
   //if ( !(convert >> int_bet) )
   if(bet==0)
   return 0;
   thePull=pull();
   mult = getPayMultiplier(thePull);
   //stringstream convert(mult);
   //if ( !(convert >> int_mult) )
   winnings=mult*bet;
   display(thePull,  winnings);
  }
  
  
  
  
  
   

int getBet()
{
   int betAmount;

   cout<<"enter bet amount (1-100), entering a zero will terminate the program: ";
   cin>>betAmount;

   if(betAmount==0)
   {
      return 0;
   }
   else if(betAmount>=1 && betAmount<=100)
   {
      return betAmount;
   }
   do
   {
      cout<<"invalid bet. please bet between 1 and 100: ";
      cin>>betAmount;
   }     
   while(betAmount>100||betAmount<0);
}


TripleString pull() 
{
   TripleString pullResult;
   cout<<"Whirrrrrrrr....and your pull is...."<<endl;
   pullResult.set(randString(),randString(),randString());
   return pullResult; 
}




string randString()
{
int randNum;

randNum=rand()%100+1;   
   if(randNum<=50)//20000*50=10000
      return "BAR";
   else if(randNum>50 && randNum<=75)//20000*25=5000
      return "cherries";
   else if(randNum>75 && randNum <=87)//20000*12.5=2500
      return "space";
   else if(randNum>87 && randNum <=100)//20000*12.5=2500
      return "7";
}


int getPayMultiplier (TripleString thePull)
{

   if(thePull.get(1)=="cherries" && thePull.get(2)!="cherries")
   {
      return 5;
   }
   else if(thePull.get(1)=="cherries" && thePull.get(2)=="cherries" && thePull.get(3)!="cherries")
   {
      return 15;
   }
   else if(thePull.get(1)=="cherries" && thePull.get(2)=="cherries" && thePull.get(3)=="cherries")
   {
   return 30;
   }
   else if(thePull.get(1)=="BAR" && thePull.get(2)=="BAR" && thePull.get(3)=="BAR")
   {
      return 50;
   }
   else if(thePull.get(1)=="7" && thePull.get(2)=="7" && thePull.get(3)=="7")
   {
      return 100;
   }
   else
   {
      return 0;
   }


void display (TripleString thePull, int winnings );
{

   //winnings = mult*bet; 
   cout<<thePull.get(1)<<"   "<<thePull.get(2)<<"   "<<thePull.get(3)<<endl;

  if( winnings != 0)
  {
  cout<<"Congratulations!  You won "<<winnings<<"!"<<endl;
  }
  else
  {
     cout<<"Sorry, you lose..."<<endl;
  }

   }
}



---------------

thanks in advance
Last edited on
please re copy your code from your compiler and paste it in code tags so it indents properly. the code tags are the <> symbol under format:
one error i can see is that at the bottom of your program, remove semicolon from the function header:

void display (TripleString thePull, int winnings );
{

//winnings = mult*bet;
cout<<thePull.get(1)<<" "<<thePull.get(2)<<" "<<thePull.get(3)<<endl;

also, there seems to be an extra bracket in this function. check it again

Edit: Actually there are other possible bracket errors in the code. Indent it and then it will be easier to debug.
Last edited on
in addition to abhishekm71's post, you should not put main in a class, the class should be in a .h file and main in a .cpp file.

also the errors you are experiencing are because of 2 things, first being what abhishekm71 said about the ; after void display (TripleString thePull, int winnings ) and also because you need a } after int getPayMultiplier (TripleString thePull) function so it would look like this

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
int getPayMultiplier (TripleString thePull)
{

if(thePull.get(1)=="cherries" && thePull.get(2)!="cherries")
{
return 5;
}
else if(thePull.get(1)=="cherries" && thePull.get(2)=="cherries" && thePull.get(3)!="cherries")
{
return 15;
}
else if(thePull.get(1)=="cherries" && thePull.get(2)=="cherries" && thePull.get(3)=="cherries")
{
return 30;
}
else if(thePull.get(1)=="BAR" && thePull.get(2)=="BAR" && thePull.get(3)=="BAR")
{
return 50;
}
else if(thePull.get(1)=="7" && thePull.get(2)=="7" && thePull.get(3)=="7")
{
return 100;
}
else
{
return 0;
}
}//Add this on the end of this function 


the code compiled for me so that should work
Last edited on
thanks so much for the assistance! the code compiles now.

@ch1156: i'm not sure how to implement the division between class and main as you've described. any pointers/internet links, etc?

it looks like there's a logic error somewhere in the code, as the program exits following one bet, rather than looping until user input==0, and i'm not sure what to modify---perhaps a do{main()} while bet!=0 ?. here's the current 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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
 #include <ctime>
#include <iostream>
#include <sstream>
#include <string.h>
#include <random>

using namespace std;




class TripleString
{

   string string1, string2, string3;

public:
   static const int MAX_LEN=20;
   TripleString();

   bool validString( string str );
   void set(string str1, string str2, string str3);
   string get(int strPosition);
};

TripleString::TripleString()
{
string1="";
string2="";
string3="";
}


bool TripleString::validString(string str)
{
   if (str.length()<=MAX_LEN)
      return true;

   return false;
}

void TripleString::set(string str1, string str2, string str3)
{
   if(validString(str1))
   {
      string1=str1;
   }
   if(validString(str2))
   {
      string2=str2;
   }
   if(validString(str3))
   {
   string3=str3;
   }
}

string TripleString::get(int pos)
{
  if(pos==1)
  {
     return string1;
  }
  if(pos==2)
  {
     return string2;
  }
  if(pos==3)
  {
     return string3;
  }
}


int getBet();

TripleString pull();

string randString();

int getPayMultiplier (TripleString thePull);

void display (TripleString thePull, int winnings );


int main()
   {
   srand( time( NULL ) );//seed for rand method   
   TripleString thePull;
   int bet, mult, winnings;
   //string mult, bet;
  
  
   bet = getBet(); 
   //stringstream convert(bet);
   //if ( !(convert >> int_bet) )
   if(bet==0)
   return 0;
   thePull=pull();
   mult = getPayMultiplier(thePull);
   //stringstream convert(mult);
   //if ( !(convert >> int_mult) )
   winnings=mult*bet;
   display(thePull,  winnings);
  }
  
  
  
  
  
   

int getBet()
{
   int betAmount;

   cout<<"enter bet amount (1-100), entering a zero will terminate the program: ";
   cin>>betAmount;

   if(betAmount==0)
   {
      return 0;
   }
   else if(betAmount>=1 && betAmount<=100)
   {
      return betAmount;
   }
   do
   {
      cout<<"invalid bet. please bet between 1 and 100: ";
      cin>>betAmount;
   }     
   while(betAmount>100||betAmount<0);
}


TripleString pull() 
{
   TripleString pullResult;
   cout<<"Whirrrrrrrr....and your pull is...."<<endl;
   pullResult.set(randString(),randString(),randString());
   return pullResult; 
}




string randString()
{
int randNum;

randNum=rand()%100+1;   
   if(randNum<=50)//20000*50=10000
      return "BAR";
   else if(randNum>50 && randNum<=75)//20000*25=5000
      return "cherries";
   else if(randNum>75 && randNum <=87)//20000*12.5=2500
      return "space";
   else if(randNum>87 && randNum <=100)//20000*12.5=2500
      return "7";
}


int getPayMultiplier (TripleString thePull)
{

   if(thePull.get(1)=="cherries" && thePull.get(2)!="cherries")
   {
      return 5;
   }
   else if(thePull.get(1)=="cherries" && thePull.get(2)=="cherries" && thePull.get(3)!="cherries")
   {
      return 15;
   }
   else if(thePull.get(1)=="cherries" && thePull.get(2)=="cherries" && thePull.get(3)=="cherries")
   {
   return 30;
   }
   else if(thePull.get(1)=="BAR" && thePull.get(2)=="BAR" && thePull.get(3)=="BAR")
   {
      return 50;
   }
   else if(thePull.get(1)=="7" && thePull.get(2)=="7" && thePull.get(3)=="7")
   {
      return 100;
   }
   else
   {
      return 0;
   }
}

void display (TripleString thePull, int winnings )
{

   //winnings = mult*bet; 
   cout<<thePull.get(1)<<"   "<<thePull.get(2)<<"   "<<thePull.get(3)<<endl;

  if( winnings != 0)
  {
  cout<<"Congratulations!  You won "<<winnings<<"!"<<endl;
  }
  else
  {
     cout<<"Sorry, you lose..."<<endl;
  }

}



Last edited on
perhaps a do{main()} while bet!=0 ?.

No, you cant a call to main inside a loop, although you do need a loop.

The following (replaces lines 94-104) should work for you:
1
2
3
4
5
6
7
  while ((bet = getBet()) > 0) 
  {  thePull=pull();
      mult = getPayMultiplier(thePull);
      winnings=mult*bet;
      display(thePull,  winnings);
  }
  return 0;


just make a .h file, the errors say your using visual studio, i dont use VS so im not sure how to add files but just google how to add a .h file to your project and then add your class to the .h file and main to the .cpp file, then in main just put #include "TripleString.h" and it sould work. if you need anymore help just let me know. as for your other problem, i would seperate main from the class and organize a little bit first.
Last edited on
Topic archived. No new replies allowed.