Slot machine help

A slot machine is a gambling device that the user inserts money into and then pulls a lever (or presses a button). The slot machine then displays a set of random images. If two or more of the images match, the user wins an amount of money that the slot machine dispenses back to the user.
Create a program that simulates a slot machine. When the program runs, it should do the following:
Asks the user to enter the amount of money he or she wants to enter into the slot machine as a bet.
Instead of displaying images, the program will randomly select a word from the following list:
Cherries, Oranges, Plums, Bells, Melons, Bars
Use an array to store this list of images
To select a word, the program will generate a random number in the range of 0 through 5.
If the number is 0 the selected word is cherries: if the numbers is 1, the selected word is Oranges; and so forth.
The program should randomly select a word from this list three times and display all three of the words. If two of the randomly selected words match, the program will inform the user that he or she has won two times the amount entered. If three of the words match, the program will inform the user that he or she has won three times the amount entered.
The program will ask whether the user wants to play again.
If so, these steps are repeated.
If not, the program displays the total amount of money entered into the slot machine and the total amount won.

Can someone help me break down how to do this problem, but the C++ version with modules that pass values that are used in other modules?

I'm super confused. I've been trying to figure this out for a week now, and now it's due today. I just am not understanding how to make this all fit together, but I do have what I need to make the program.

Beyond these steps I'm lost.
1.ask user for money
2. generate six random numbers between 1-6
3. make switch with different words (cherriers, oranges plums, bells, melons, bars)


http://www.leveluplunch.com/java/exercises/slot-machine-simulation/
Last edited on
It is actually very simple, look they have also given you the steps for implementation.

"Asks the user to enter the amount of money he or she wants to enter into the slot machine as a bet. "

- Make a variable called bet and have the user enter an integer value in it. As long as the value inputted by the user is not greater than the total amount of cash the user currently has, the bet is legal, otherwise, ask for another bet.

Instead of displaying images, the program will randomly select a word

- Here is a helpful habit you should develop as you progress as a software developer. The moment you read the word 'randomly' in a question, you should immediately start thinking about random number generation which I can see that you did according to the steps you have been able to figure out yourself, so good job. To tackle this step, you may use enum as a way to represent the words, generate a random number between 0 and 5 (since by default the first element in an enumerator is equal to 0), then using a switch-case you may display a certain image. If you cannot get away with this without using arrays, then simply replace the enumerator with an array of 5 integers. Remember that you will need a loop that loops at least 3 times and also remember to generate a random number for each iteration of the loop. Your random number generator should look like this:

1
2
3
srand(static_cast<unsigned int>(time(NULL)));
//Game loop begins...
 randomNum = rand() % 6;


To detect whether there have been a match, simply use if-statements like so:
1
2
3
4
5
if (a == b && a == c && b == c)
{
cout << "You have successfully spun three identical images! Well done!" << endl;
// Reward the player by giving them *3 the amount of their bet.
}


Hopefully that clarified few things for you and gave you a place to start. If you are still confused, post below and I will do my best to get back to you. Note that I am not very active but there are a lot of brainiacs on this forum who would gladly help out.
Last edited on
Give me a moment. I'm not at home yet.

I'll write up what's confusing me.

And thank you!
From a first look i would tackle it something like this:
I would recommend a do-while-loop.
something on the lines of
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

 do{
 
cout << "Wanna play? (Y/N)" << endl;
cin >> play;
 if(play == 'Y')
{
// code for the generator, comparing etc., all the good stuff
}
 else if(play =='N') VariableForProof=false;
 else cout <<"invalid entry" << endl;
}while(VariableForProof == true);

//Now the part where you tell the user how much he entered in total 
//and how much he won 


Sorry, if that takes away too much work or brainpower, or if it doesn't help at all.

Generally, getting a structorizer or something else to plan a project before starting to write it always helps clarify stuff. Write Pseudo-Code first, then translate it to actual Code.
E.g:
Ask user how old he is and how old his dad is. Save both Values, then calculate how old his dad was when he was born.
Translates to:
1
2
3
4
5
cout << "How old are you?" << endl;
cin >> UserAge;
cout << "How old is your Dad? << endl;
cin >> DadAge;
cout << "Your Dad was " << DadAge-UserAge << " Years old when you were born" << endl; 

Yes its a stupid example, but i hope you get what i mean ;)
Alright, I'm still super confused, but I figured the best way to figure it out is to start doing the program.

I got my variables.
I got them randomized.

I got 158, 1000, and 140.

The original amount I put in was 100.

The total amount of money and the amount of money entered by the user aren't showing up correctly when ran.

Sometimes, the total and the amount entered displays correctly, it should be noted.

But not always, and that's a problem.

There is some logic error(s) I can't figure out.

I'll keep toying and updating 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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <stdlib.h>
using namespace std;

void switchStatementsCalculations (int &slot1, int &slot2, int &slot3, string cherries, string
                                   oranges, string plums, string bells, string melons, string bars);

void calculateAmountEarnedByPlaying (double &money, int slot1, int slot2, int slot3,
                                     double &total);

int main()
{
    int slot1;
    int slot2;
    int slot3;

    double money;
    double total=0;
    double amountOfMoneyEnterd=0;
    int count;

    string cherries = "cherries";
    string oranges = "oranges";
    string plums = "plums";
    string bells = "bells";
    string melons = "melons";
    string bars = "bars";
    string doAgain;

    do
    {

        cout << "We are going to be playing a slot machine game today." << endl;
        cout << "Please enter the amount of money you'd like to insert into the slot machine. We will pull the lever for you." << endl;
        cin >> money;
        cout << "You put in $" << money << endl;


        srand(time(0));

        slot1=rand()%6+1;
        slot2=rand()%6+1;
        slot3=rand()%6+1;

        switchStatementsCalculations(slot1, slot2, slot3, cherries, oranges, plums, bells, melons, bars);

        calculateAmountEarnedByPlaying(money, slot1, slot2, slot3, total);

        amountOfMoneyEnterd=(amountOfMoneyEnterd+money);





        cout << "Would you like to play again? Please type yes if so." << endl;
        cin >> doAgain;
        if(doAgain!= "yes")
        {
            cout << "The total amount of money you put in the slot machine is " << amountOfMoneyEnterd << endl;
            cout << "The total amount of money you won is $" << total << endl;
        }

    }
    while(doAgain=="yes");


    system("Pause");
    return 0;
}

void switchStatementsCalculations(int &slot1, int &slot2, int &slot3, string cherries, string
                                  oranges, string plums, string bells, string melons, string bars)
{
    switch (slot1)
    {
    case 1:
        cout << "You got " << cherries << endl;
    case 2:
        cout << "You got " << oranges << endl;
        break;
    case 3:
        cout << "You got " << plums << endl;
        break;
    case 4:
        cout << "You got " << bells << endl;
        break;
    case 5:
        cout << "You got " << melons << endl;
        break;
    case 6:
        cout << "You got " << bars << endl;
    }

    switch (slot2)
    {
    case 1:
        cout << "You got " << cherries << endl;
        break;
    case 2:
        cout << "You got " << oranges << endl;
        break;
    case 3:
        cout << "You got " << plums << endl;
        break;
    case 4:
        cout << "You got " << bells << endl;
        break;
    case 5:
        cout << "You got " << melons << endl;
        break;
    case 6:
        cout << "You got " << bars << endl;

    }

    switch (slot3)
    {
    case 1:
        cout << "You got " << cherries << endl;
        break;
    case 2:
        cout << "You got " << oranges << endl;
        break;
    case 3:
        cout << "You got " << plums << endl;
        break;
    case 4:
        cout << "You got " << bells << endl;
        break;
    case 5:
        cout << "You got " << melons << endl;
        break;
    case 6:
        cout << "You got " << bars << endl;

    }
}

void calculateAmountEarnedByPlaying(double &money, int slot1, int slot2, int slot3, double &total)

{
    double won;

    if(slot1==slot2 || slot1==slot3 || slot2==slot3)
    {
        cout << "Congratulations! You won." << endl;
        won=(money * 2);
        cout << "You won " << won << endl;
    }


    else if ((slot1==slot2 && slot1==slot3) || (slot2==slot1 && slot2==slot3) || (slot3==slot1 && slot3==slot2))
    {
        cout << "Congratulations! You won." << endl;
        won=(money*3);
        cout << "You won " << won << endl;
    }
    else
    {
        cout << "You didn't earn any money." << endl;
    }

    total=(total+won);
}
Last edited on
line 145 double won; should be double won=0;
Well, I had to do a ton of editing, but I think everything is finally alright. Gonna add comments and submit. My issue was I didn't initialize money or won to 0.

You already pointed that out, though. If you think I did anything wrong, feel free to comment.

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

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <stdlib.h>
using namespace std;

void switchStatementsCalculations (int &slot1, int &slot2, int &slot3, string cherries, string
                                   oranges, string plums, string bells, string melons, string bars);

void calculateAmountEarnedByPlaying (double &money, int slot1, int slot2, int slot3,
                                     double &total);

int main()
{
    int slot1;
    int slot2;
    int slot3;

    double money=0;
    double total=0;
    double amountOfMoneyEnterd=0;
    int count;

    string cherries = "cherries";
    string oranges = "oranges";
    string plums = "plums";
    string bells = "bells";
    string melons = "melons";
    string bars = "bars";
    string doAgain;

    cout << "We are going to be playing a slot machine game today." << endl;
    srand(time(0));

    do
    {

        cout << "Please enter the amount of money you'd like to insert into the slot machine. We will pull the lever for you." << endl;
        cin >> money;
        cout << "You put in $" << money << endl;




        slot1=rand()%6+1;
        slot2=rand()%6+1;
        slot3=rand()%6+1;

        switchStatementsCalculations(slot1, slot2, slot3, cherries, oranges, plums, bells, melons, bars);

        calculateAmountEarnedByPlaying(money, slot1, slot2, slot3, total);

        amountOfMoneyEnterd=(amountOfMoneyEnterd+money);





        cout << "Would you like to play again? Please type yes if so." << endl;
        cin >> doAgain;
        if(doAgain!= "yes")
        {
            cout << "The total amount of money you put in the slot machine is " << amountOfMoneyEnterd << endl;
            cout << "The total amount of money you won is $" << total << endl;
        }

    }
    while(doAgain=="yes");


    system("Pause");
    return 0;
}

void switchStatementsCalculations(int &slot1, int &slot2, int &slot3, string cherries, string
                                  oranges, string plums, string bells, string melons, string bars)
{
    switch (slot1)
    {
    case 1:
        cout << "You got " << cherries << endl;
    case 2:
        cout << "You got " << oranges << endl;
        break;
    case 3:
        cout << "You got " << plums << endl;
        break;
    case 4:
        cout << "You got " << bells << endl;
        break;
    case 5:
        cout << "You got " << melons << endl;
        break;
    case 6:
        cout << "You got " << bars << endl;
    }

    switch (slot2)
    {
    case 1:
        cout << "You got " << cherries << endl;
        break;
    case 2:
        cout << "You got " << oranges << endl;
        break;
    case 3:
        cout << "You got " << plums << endl;
        break;
    case 4:
        cout << "You got " << bells << endl;
        break;
    case 5:
        cout << "You got " << melons << endl;
        break;
    case 6:
        cout << "You got " << bars << endl;

    }

    switch (slot3)
    {
    case 1:
        cout << "You got " << cherries << endl;
        break;
    case 2:
        cout << "You got " << oranges << endl;
        break;
    case 3:
        cout << "You got " << plums << endl;
        break;
    case 4:
        cout << "You got " << bells << endl;
        break;
    case 5:
        cout << "You got " << melons << endl;
        break;
    case 6:
        cout << "You got " << bars << endl;

    }
}

void calculateAmountEarnedByPlaying(double &money, int slot1, int slot2, int slot3, double &total)

{
    double won=0;

    if(slot1==slot2 || slot1==slot3 || slot2==slot3)
    {
        cout << "Congratulations! You won." << endl;
        won=(money * 2);
        cout << "You won " << won << endl;
    }


    else if ((slot1==slot2 && slot1==slot3) || (slot2==slot1 && slot2==slot3) || (slot3==slot1 && slot3==slot2))
    {
        cout << "Congratulations! You won." << endl;
        won=(money*3);
        cout << "You won " << won << endl;
    }
    else
    {
        cout << "You didn't earn any money." << endl;
    }

    total=(total+won);
}
Last edited on
nice code... :) just document it as everyone loves documentation
Topic archived. No new replies allowed.