Have a few questions about C++

Hello all,
I am fairly new to C++ and am having a problem with one of my assignments.
It involves making a slot machine that generates random numbers. One of the problems that I am having is the random numbers do not generate my out put always comes up as [0][0][0] so it always hits jackpot. Another concern I have is that I think that I need to uses a case structure where the payout is calculated but not sure how to do that. Here is my 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
#include <iostream>
#include <ctime> 

using namespace std; 

class slotMachine {
private:
int wheelA;
int wheelB;
int wheelC;
double payOut; //amount of $ won during the current turn
double moneyInMachine; //total amount of $ in the machine
double playersBalance; //the amount of $ not yet played
double gameCost; //the cost of one pull
double moneyPaid; //the money put in by user
public:
slotMachine();
bool displayMenu(void);
bool pullHandle(void);
void spinWheel(int &);
double calculatePayout();
void insertCoin(double );
void displaySpinResults();
int Random(int, int);
void displayTotals();
}; 

int main(void) {
//create a slot machine object 
slotMachine mySlot; 

//Start the slot machine game
//Keep running until the user
//decides to quit 

bool ok = true;
while (ok){
ok = mySlot.displayMenu();
}; 

return 0; 
} 

slotMachine::slotMachine () {
//constructor, set the initial state
//of all the properties 

srand((int) time(0));
moneyInMachine = 100;
moneyPaid = 0;
payOut = 0;
wheelA = 0;
wheelB = 0;
wheelC = 0;
gameCost = 1;
} 

bool slotMachine::displayMenu(void){
//main menu, executes the command selected by the
//user or returns a value to terminate game execution

char choice = 'Z'; 

bool continueGame = true; 

cout << "\n\n(E)nd, (P)ull, P(A)Y, (T)otals :"; 
cin >> choice; 

switch (choice) {
case 'e':
case 'E': //stop game 
continueGame = false; 
break; 
case 'a':
case 'A': //pay 
double money; 
cout << "\nIt's a dollar a pull!\n" 
<< "Put money into the machine $"; 
cin >> money; 
insertCoin(money); 
break; 
case 'p':
case 'P': //pull 
if (pullHandle()){ 
cout << endl << endl << endl; 
displaySpinResults(); 
cout << "Payout $" << calculatePayout(); 
} 
break;
case 't':
case 'T': //display totals 
displayTotals();
break;
}
return continueGame; 
} 

bool slotMachine::pullHandle(void){
//checks to see if there is enough user money 
//for a spin. If so, assigns a random value
//to each wheel. Deducts the cost of one
//pull from the users money. 
//returns true if the handle was pulled.
//returns false if the handle could not be pulled 
double moneyInMachine = 25;
int wheelA = 1;
int wheelB = 2;
int wheelC = 3;

 playersBalance = moneyInMachine - moneyPaid;
 cout << "You have " << playersBalance << endl;

return true;

} 

void slotMachine::spinWheel(int &theWheel){
//assign a random value to a wheel 
int wheelA = rand();
int wheelB = rand();
int wheelC = rand();
}
int rand();
{
int wheelA;
wheelA = 1 + rand() % (3 - 1 + 1 );
int wheelB;
wheelB = 1 + rand() % (3 - 1 + 1);
int wheelC;
wheelC = 1 + rand() % (3 - 1 + 1);
}

double slotMachine::calculatePayout(){
//decides if the current wheel values merit a 
//payout and how much that payout should be.
//deducts the total payout from the total
//amount of money in the machine
int jackPot = 1000;
int goodJob = 10;
int youLose = 5;
int wheelA = rand();
int wheelB = rand();
int wheelC = rand();

if (wheelA == wheelB && wheelA == wheelC)
{
	playersBalance = moneyInMachine + jackPot;
	cout << "Jackpot!!!  " << jackPot << endl;
	
}
 else if (wheelA == wheelB != wheelC || wheelA == wheelC != wheelB || wheelB == wheelC != wheelA)
{
	playersBalance = moneyInMachine + goodJob;
	cout << "Good Job  " << goodJob  << endl;
	
}

 else (wheelA != wheelB != wheelC || wheelA != wheelC != wheelB || wheelB != wheelC != wheelA);
{
	playersBalance = moneyInMachine - youLose;
	cout << "You Lose,Play Again" << endl;
	cout << " You have    " << playersBalance << endl;
	
}
return moneyInMachine;


} 

void slotMachine::insertCoin(double amount = 0){
//adds to the amount of money paid by the user
//adds to the total money in the machine
int moneyInMachine = 25;
int moneyPaid = 0;

moneyInMachine = moneyPaid + moneyInMachine;

} 

void slotMachine::displaySpinResults(){
//displays the value of the three wheels
cout << "[" << wheelA << "] " 
<< "[" << wheelB << "] " 
<< "[" << wheelC << "] \n\n" ; 
} 

void slotMachine::displayTotals(){
//displays the amount of money and number
// of pulls the player has left
cout << "\nMoney in Machine $" << slotMachine::moneyInMachine << endl;
cout << "Pulls Left: " << slotMachine::gameCost / slotMachine::moneyPaid << endl << endl; 
} 

int slotMachine::Random(int lowerLimit, int upperLimit) {
//returns a random number within the given boundary
 return 1 + rand() % (upperLimit - lowerLimit + 1); 
}
Last edited on
Please remove that code, type "[code]", paste your code in, and then type "[/code]". It keeps your original formatting. So this:
#include <iostream>

int main() {
std::cout << "Hello, world!" << std::endl;
std::cin.get();
return 0;
}

becomes this:
1
2
3
4
5
6
7
#include <iostream>

int main() {
    std::cout << "Hello, world!" << std::endl;
    std::cin.get();
    return 0;
}


You could use this random number generator. It's fairly simple yet very effective:
1
2
    int randNum = 0, min = 0, max = 9, range = (max - min) + 1;
    randNum= min + int(range * rand() / RAND_MAX + 1.0));


Try seeding rand like this:

 
srand((unsigned)time(0)); 

and put your seed statement below your #includes aswell.

I got that code from here: http://www.daniweb.com/forums/thread1769.html
a while ago, and I've been using it ever since. It's very good. Just change the value of min and max. Obviously don't just copy and paste it; rewrite it and try to understand it...
Last edited on
ok I have redid my code and also made a few changes to it but still having trouble with generating the random numbers
Try the above random number generator.

Add some cout statements so you can see what happens at each point.
Yes that would work but there are some things in the code that I cannot change because it was code given for my assignment.
1
2
#include <ctime> 
srand((int) time(0));

and doubt I can change those as it would result in not doing my assignment right.
Alternative way to use rand that doesn't involve converting to and from floating point:

1
2
3
4
int min = 0, max = 10;
int range = max-min;

int randNum = (rand() % range) + min;


Note this returns a number between [min..max). IE, max is exclusive, so with the above you'd get 9, but never 10.
I give up I cannot make this code run the way it is supppose to. Been working on this since 10 am yesterday and haven't really gotten much farther than waht I was then. Thanks everyone whom replied for your help.
Topic archived. No new replies allowed.