Slot Machine Practice Problem

Hi all
I am writing a slot machine program as part of a book i am following.
It wants me to randomly display the results of the slot machine to the player, have 3 or more values for each wheel of the machine.
I'm having trouble getting the program to check user input and randomly spin out the results.

code below:
//Slot Machine Practice Problem- 30/08/2018
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;

class slotMachine
{
public:
int Exit()
{
cout << "Thank you for playing, see you again!";
}
int slotRoll()
{
cout << "Spinning... \n";
//rand(slotVal[0,1,2,3]);
int result;
result = rand()%4;
for (int i = 0; i < 4; i++)
{
result = (rand()%4)+1;
}
cout << result;
}


};


int main()
{
srand(time(nullptr));
slotMachine slotPlayer;
string slotVal [4] = {"Coin", "Bomb", "Rainbow", "Rope"};
int input;
cout << "Welcome to Slot Machine! \n";
cout << "1. Play \n";
cout << "2. Exit \n";
cout << "Please enter option: ";
cin >> input;
switch(input)
{
case 1:
slotPlayer.slotRoll();
break;
case 2:
slotPlayer.Exit();
break;
default:
cout << "Error! Bad input! Terminating program.";
}
if (slotVal[0] != slotVal[1] && slotVal[1] != slotVal[2] && slotVal[2] != slotVal[3])
{
cout << "Bad Luck, No matches this round. \n";
}
if (slotVal[0] == slotVal[1] && slotVal[1] == slotVal[2] && slotVal[2] == slotVal[3])
{
cout << "You Won and got a match! Congratulations! \n";
}

}

any help would be appreciated.
Cheers
cout << result; needs to be inside your for loop.
you also need to return some values for main() and Exit()

http://cpp.sh/96i3x
thanks for that, i got a mate to look at my code and made some revisions, the comment where he says to '//connect this function with the rest of the program'. i'm not to sure on how that is meant to be done precisely.
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;

class slotMachine
{
public:
void Exit()
{
cout << "Thank you for playing, see you again!" << endl;
}
void slotRoll()
{
cout << "Spinning... \n";
int result;
for (int i = 0; i < 4; i++)
{
result = (rand()%4)+1; // You need to connect this function with the rest of the program.
cout << result << " ";
}
cout << "" << endl;
}
};

int main()
{
slotMachine slotPlayer;
const string slotVal [4] = {"Coin", "Bomb", "Rainbow", "Rope"};
int input;

cout << "Welcome to Slot Machine! \n";
cout << "1. Play \n";
cout << "2. Exit \n";
cout << "Please enter option: ";
cin >> input;

switch(input)
{
case 1:
slotPlayer.slotRoll();
break;
case 2:
slotPlayer.Exit();
break;
default:
cout << "Error! Bad input! Terminating program.";
break;
}
system("pause");
}
I think your mate means that slotRoll() generates the reel values but does nothing with them other than print out the values. Somehow you need to get those numbers converted to the text in slotVal[] before displaying to the user.

another point of note is that you don't "seed" the random number generator, therefore it is seeded with 0 by default. Each time you run your program you will probably get the same results. At the beginning of main() call srand(clock()); to ensure the random number sequence is different each time you run your program.


slotVal should be a member of class slotMachine.

I think slotMachine should have a vector<int> that contains the numbers most recently rolled. You'll undoubtedly need this later when you have to compute the payout for a roll.
Topic archived. No new replies allowed.