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
#include <iostream>
#include <ctime>
usingnamespace 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;
returntrue;
}
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;
}
elseif (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);
}
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...
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.