Plinko - part 2!

So I got some good help from my last post!!
(http://www.cplusplus.com/forum/beginner/79921/)

And was able to figure out my problem!

But now I've ran into a few more....

Again, looking for hints, or syntax I should look up, or something.

Now, the program is broken basically into 3 parts.
A - dropping 1 token in the plinko machine and show your winnings
B - dropping multiple tokens in the same slot of the plinko machine then report the total average $ of those tokens.
C - Exit the program

So I have A and C working (I know, using goto statements is considered taboo, but I already had most of it written when they changed the originally assignment on me, so I just threw in some goto's)

I'm working on part B.
I can't seem to figure out how to throw multiple coins, into the same slot, and then compare their winnings. I was trying to do a loop inside of a loop. So basically trying to run part A x amount of times (depending on user input).

Anyhow, would love if you could take a look. If you have any further questions, contact me.

Thanks!

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
#include <iostream>
#include <math.h>
#include <string>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <cstdlib>
#include <ctime>
using namespace std;

int main(){

    double pz0 = 100;
    double pz1 = 500;
    double pz2 = 1000;
    double pz3 = 0;
    double pz4 = 10000;
    double pz5 = 0;
    double pz6 = 1000;
    double pz7 = 500;
    double pz8 = 100;
    string option_select;
    double slot;
    string yes_no;
    srand (time(0));
    double mult_token;
    double mult_slot;

    cout <<"********  PLINKO  SIMULATOR  ********\n\n";
    cout <<"Please select from the following options:\n";
    cout <<"A: Drop one token\n";
    cout <<"B: Drop multiple tokens\n";
    cout <<"C: Quit the program\n";
    cin >> option_select;
    goto program;

    start:
    cout <<"\n\nThat was an invalid inplut\n";
    cout <<"********  PLINKO  SIMULATOR  ********\n\n";
    cout <<"Please select from the following options:\n";
    cout <<"A: Drop one token\n";
    cout <<"B: Drop multiple tokens\n";
    cout <<"C: Quit the program\n";
    cin >> option_select;


// Part A
    program:
    if (option_select != "A" && option_select !="a" && option_select !="B"
           && option_select !="b" && option_select !="C" && option_select !="c")
    {
        cout <<"**That is not a valid input, please try again**\n\n";
        cout <<"********  PLINKO  SIMULATOR  ********\n\n";
        cout <<"Please select from the following options:\n";
        cout <<"A: Drop one token\n";
        cout <<"B: Drop multiple tokens\n";
        cout <<"C: Quit the program\n";
        cin >> option_select;
    }

    if (option_select == "A" || option_select == "a")
    {
        cout <<"\nThere are 8 slots in which you can drop your token!\n";
        cout <<"In which slot will we be dropping your token?\n";
        cout <<"Slot: ";
        cin >> slot;

        while (slot >=9 || slot <= -1)
        {
            goto start;
        }
        if (slot <=8 || slot >=0)
        {
            double pos = slot;
            for (int i=1; i<=12; i++)
            {
                double plink = (rand () %2) -0.5;
                pos = pos + plink;
                if (pos > 8)
                {
                    pos = pos - 1.0 ;
                }
                if (pos < 0)
                {
                    pos = pos + 1.0;
                }
                cout << pos << "  **PLINK**  \n" << endl;
            }
            cout << "You landed at: " << pos << endl;
            if (pos == 0)
            {
                cout << "You win: " << pz0;
            }
            else if (pos == 1)
            {
                cout << "You win: " << pz1;
            }
            else if (pos == 2)
            {
                cout << "You win: " << pz2;
            }
            else if (pos == 3)
            {
                cout << "You win: " <<pz3;
            }
            else if (pos == 4)
            {
                cout << "You win: " <<pz4;
            }
            else if (pos == 5)
            {
                cout << "You win: " <<pz5;
            }
            else if (pos == 6)
            {
                cout << "You win: " <<pz6;
            }
            else if (pos == 7)
            {
                cout << "You win: " <<pz7;
            }
            else if (pos == 8)
            {
                cout << "You win:  " <<pz8;
            }
                cout << " ";
        }
    }
    
//B's options
    if (option_select == "B" || option_select == "b")
    {
        cout << "\nHow many tokens would you like to drop? ";
        cin >> mult_token;
        while (mult_token <= 0)
        {
            goto start;
        }


        cout << "\nIn which slot (0 - 8) would you like to drop your " << mult_token << " token(s)? ";
        cin >> mult_slot;
        while (mult_slot >=9 || mult_slot <= -1)
        {
            goto start;
        }

        if (mult_token >= 0 && mult_slot <=8 && mult_slot >=0)
        {
            for (int s=1; s=mult_token; s++)
            {
                for (int i=1; i<=12; i++)
                {
                    double pos = mult_slot;
                    double plink = (rand () %2) -0.5;
                    pos = pos + plink;
                    if (pos > 8)
                    {
                        pos = pos - 1.0 ;
                    }
                    if (pos < 0)
                    {
                        pos = pos + 1.0;
                    }
                        cout << pos << "  **PLINK**  \n" << endl;
                }
            }
    }



//C's option
    if (option_select == "C" || option_select == "c")
    {
        end:
        cout <<"\n\n****************************************\n";
        cout <<"May the odds forever be in your favor!!!\n";
        cout <<"****************************************\n\n";

    }
    return 0;
}}
for (int s=1; s=mult_token; s++) in line 153

Should be s==mult_token;.
Did your instructor/learning resources teach you about functions?

The reason I ask is because of the gotos on lines 71 and 146, which is considered by many programmers to be really evil. Why? Because it makes code harder to read and follow. This would be the perfect type of program to use functions in. You know, one function for each of the parts, with another function for the actual "plinking". From there, it would be very easy to run the "plinking" in part B multiple times.

Also, your plinko machine has 9 slots, not 8. See lines 149 and 78. If 0-8 are all valid options, than that makes 9 options. 1-8 would give you 8 options. ;)

I hope that all helps!

-Albatross
Last edited on
@ Albatross - Yeah, I heard the goto statements were an evil thing, and I can understand how now that I've done this all. I already had the bulk of the program when he decided to change a part of it, so to save on time I just threw in the goto statements. He hasn't actually gone over functions yet. I know they would make my life easier though. I think he did that on purpose though, so that we can see how much it sucks w/o functions. But I do not know his methodology :-x.
Oh, the machine is meant to have 9 input slots, slot 0 - slot 8. I asked why it didn't start at 1, he didn't like that question.

@ Aceix - That helped with the over flow issue. But I also need to be able to see each of the results, and then compare their ending locations to the amount of money. Comparing it to my old code, the
cout << pos would display falling locations. This time I can't get it to display anything.


I guess what I'm not understanding is how to have the user choose a number, then have the function (which adds +.5 or -.5 randomly 12 times) run itself that many times (that the user chose) and spit out those answers.
So if he chooses to drop 5 tokens in slot 2. I need to know where those 5 tokens land. Compare those locations to the money they should win. Then average it.

Thanks :-). I'll probably be heading up to a lab session in a few hours too, but any help I can get would make it much easier up there :-)
Update!!

If anyone cares...

So up in line 28 I had mult_slot declared as a double, and that screwed up my line 151 in the for loop.

Fixed :-).

Programming can be evil sometimes :-x.....
Topic archived. No new replies allowed.