Plinko help

Hello,

I am new to coding and could use some help to understanding what I am doing wrong. I have been trying to fix the errors, but seem unable to do so on my own. Any help or advice would be much appreciated.

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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
  #include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <string>
using namespace std;
const double PRIZE_0 = 100.0;
const double PRIZE_1 = 500.0;
const double PRIZE_2 = 1000.0;
const double PRIZE_3 = 0.00;
const double PRIZE_4 = 10000.0;
const double PRIZE_5 = 0.00;
const double PRIZE_6 = 1000.0;
const double PRIZE_7 = 500.0;
const double PRIZE_8 = 100.0;
const int QUIT = 0;

int main () {
  
   cout << endl;
   cout << " 0 - Quit the program" << endl;
   cout << " 1 - Drop a single chip into one slot" << endl;
   cout << " 2 - Drop multiple chips into one slot" << endl;
   cout << endl;
   cout << "Enter your selection now: ";
}
int prize_money(int location_of_chip)
{
   int reward = 0;
   switch (location_of_chip)
   {
   case 0:
       location_of_chip = 0;
       reward = PRIZE_0;
       break;
   case 1:
       location_of_chip = 1;
       reward = PRIZE_1;
       break;
   case 2:
       location_of_chip = 2;
       reward = PRIZE_2;
       break;
   case 3:
       location_of_chip = 3;
       reward = PRIZE_3;
       break;
   case 4:
       location_of_chip = 4;
       reward = PRIZE_4;
       break;
   case 5:
       location_of_chip = 5;
       reward = PRIZE_5;
       break;
   case 6:
       location_of_chip = 6;
       reward = PRIZE_6;
       break;
   case 7:
       location_of_chip = 7;
       reward = PRIZE_7;
       break;
   case 8:
       location_of_chip = 8;
       reward = PRIZE_8;
       break;
   }
   return(reward);
}
int drop_single_chip(int slot_number, int option) {
   double location_of_chip = slot_number;
   for (int i = 0; i < 12; i++)
   {
       int random_number = rand();
       if (random_number % 2 == 1)
       {
           location_of_chip += 0.5;
           if (location_of_chip > 8)
           {
               location_of_chip -= 1.0;
           }
       }
       else
       {
           location_of_chip -= 0.5;
           if (location_of_chip < 0)
           {
               location_of_chip += 1.0;
           }
       }
       if (option == 1)
       {
           cout << " " << location_of_chip;
       }
   }
   int winnings = prize_money(location_of_chip);
   return winnings;
}
int drop_multiple_chips(int slot_number, int number_of_chips, int option)
{
   double total_winnings = 0;
   double location_of_chip = slot_number;
   for (int i = 0; i < number_of_chips; i++)
   {
       int winnings = drop_single_chip(slot_number, option);
       total_winnings += winnings;
   }
   return total_winnings;
}
void drop_multiple_chips_in_each_slot(int number_of_chips_per_slot, int option)
{
   for (int slot_number = 0; slot_number < 9; slot_number++)
   {
       double total_winnings = drop_multiple_chips(slot_number, number_of_chips_per_slot, option);
       cout << fixed << setprecision(2);
       cout << "Total Winnings on slot " << slot_number << " " << "chips: " << total_winnings << endl;
       double average_winnings_per_chip = total_winnings / number_of_chips_per_slot;
       cout << "Average winnings per chip: " << average_winnings_per_chip << endl << endl;
   }
}
int main()
{
   int option;
   srand(time(0));
   cout << "Welcome to the Plinko simulator!" << endl << endl;
   do {
       //root_menu();
       cin >> option;
       if (cin.fail()) {
           cin.clear();
           cin.ignore(1000);
           cout << "Invalid selection. Please enter 0, 1, or 2" << endl;
       }
       else {
           switch (option)
           {
           case 1:
               cout << "*** DROP SINGLE CHIP ***" << endl << endl;
               cout << endl << "Which slot do you want to drop the chip(s) in (0-8)? " << endl;
               int slot_number;
               cin >> slot_number;
               if (cin.fail())
               {
                   cin.clear();
                   cin.ignore(1000);
                   cout << "Invalid slot." << endl;
               }
               else if (slot_number > 8 || slot_number < 0)
               {
                   cout << "Invalid slot." << endl;
               }
               else
               {
                   //double slot_number_double = slot_number;
                   cout << fixed << setprecision(1);
                   int winnings = (slot_number, option);
                   cout << "Winnings: $" << winnings << endl << endl;
               }
               break;
           case 2:
               cout << endl << "*** DROP MULTIPLE CHIPS ***" << endl << endl;
               cout << "How many chips do you want to drop (>0)? " << endl;
               int number_of_chips;
               cin >> number_of_chips;
               if (cin.fail())
               {
                   cin.clear();
                   cin.ignore(1000);
                   cout << "Invalid number of chips." <<endl;
               }
               else if (number_of_chips < 1)
               {
                   cout << "Invalid number of chips." << endl;
               }
               else
               {
                   cout << endl << "Which slot do you want to drop the chip(s) in (0-8)? " << endl;
                   int slot_number;
                   cin >> slot_number;
                   if (cin.fail())
                   {
                       cin.clear();
                       cin.ignore(1000);
                       cout << "Invalid slot. Please enter an integer between 0 and 8" << endl;
                   }
                   else if (slot_number > 8 || slot_number < 0)
                   {
                       cout << "Invalid slot. Please enter an integer between 0 and 8" << endl;
                   }
                   else
                   {
                       double total_winnings = drop_multiple_chips(slot_number, number_of_chips, option);
                       cout << fixed << setprecision(2);
                       cout << "Total winnings on " << number_of_chips << " chips: $" << total_winnings << endl;
                       double average_winnings_per_chip = total_winnings / number_of_chips;
                       cout << "Average winnings per chip: $" << average_winnings_per_chip << endl << endl;
                   }
               }
               break;
           case 3:
               cout << endl << "How many chips do you want to drop (>0)? ";
               int number_of_chips_per_slot;
               cin >> number_of_chips_per_slot;
               cout << endl;
               if (cin.fail())
               {
                   cin.clear();
                   cin.ignore(1000);
                   cout << "Invalid number of chips.";
               }
               else if (number_of_chips_per_slot <= 0)
               {
                   cout << "Invalid number of chips.";
               }
               else
               {
                   cout << "*** SEQUENTIALLY DROP MULTIPLE CHIPS ***" << endl << endl;
                   drop_multiple_chips_in_each_slot(number_of_chips_per_slot, option);
               }
               break;
           case 4:
               cout << "Goodbye!" << endl;
               return 0;
               break;
           default:
               cout << endl<< "Invalid selection. Please enter 0, 1, or 2" << endl;
               break;
           }
       }
   }
   while (option != 0);
   //system("pause");
   return 0;
}
First of look at line 122 which says

int main()
{

....
}

You already have a main function in the program that contains a menu. That would be your problem number 1.

Problem 2 is that you mix int's and doubles. For instance you declare const doubles as globals at the top of your program. Then, in another function, you declare an int variable and mix int with double - which gives warnings about possible data loss, so you better use one or the other.

Then at least you should see your basic menu, not much beyond that seems to be working in the current state your program is in. Where, for instance, do you call any functions that do something from main? Other than displaying the menu, of course. ;)

Run the rest of the program through the debugger a number of times and see what else does not work, or should be changed/rearranged, so that your programs functions as it should.
Last edited on
Topic archived. No new replies allowed.