UPDATED PLINKO SIMULATION

I need help with formatting. I cant get my winnings from path of a single drop get .00, and also i think my whole loop condition isnt doing what i want it to do.

This is how the output should look like

MENU: Please select one of the following options:

0 - Quit the program
1 - Drop a single chip into one slot
2 - Drop multiple chips into one slot

Enter your selection now: -1

INVALID SELECTION. Please enter 0, 1 or 2

MENU: Please select one of the following options:

0 - Quit the program
1 - Drop a single chip into one slot
2 - Drop multiple chips into one slot

Enter your selection now: 3

INVALID SELECTION. Please enter 0, 1 or 2

MENU: Please select one of the following options:

0 - Quit the program
1 - Drop a single chip into one slot
2 - Drop multiple chips into one slot

Enter your selection now: 1

*** DROP SINGLE CHIP ***

Which slot do you want to drop the chip in (0-8)? -1

INVALID SLOT.

MENU: Please select one of the following options:

0 - Quit the program
1 - Drop a single chip into one slot
2 - Drop multiple chips into one slot

Enter your selection now: 1

*** DROP SINGLE CHIP ***

Which slot do you want to drop the chip in (0-8)? 9

INVALID SLOT.

MENU: Please select one of the following options:

0 - Quit the program
1 - Drop a single chip into one slot
2 - Drop multiple chips into one slot

Enter your selection now: 1

*** DROP SINGLE CHIP ***

Which slot do you want to drop the chip in (0-8)? 5

*** DROPPING CHIP INTO SLOT 5 ***
PATH: [5.0 4.5 4.0 4.5 5.0 4.5 4.0 4.5 4.0 4.5 5.0 4.5 5.0]
WINNINGS: $0.00

MENU: Please select one of the following options:

0 - Quit the program
1 - Drop a single chip into one slot
2 - Drop multiple chips into one slot

Enter your selection now: 2

*** DROP MULTIPLE CHIPS ***

How many chips do you want to drop (>0)? -1

INVALID NUMBER OF CHIPS.

MENU: Please select one of the following options:

0 - Quit the program
1 - Drop a single chip into one slot
2 - Drop multiple chips into one slot

Enter your selection now: 2

*** DROP MULTIPLE CHIPS ***

How many chips do you want to drop (>0)? 142

Which slot do you want to drop the chip in (0-8)? 7

Total Winnings on 142 chips: 115100.00
Average winnings per chip: 810.56

MENU: Please select one of the following options:

0 - Quit the program
1 - Drop a single chip into one slot
2 - Drop multiple chips into one slot

Enter your selection now: 0

GOODBYE!

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
236
#include <iostream>
#include <string>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <iomanip>
#include <stdlib.h>

using namespace std;

const double PRIZE_0 = 100.00;
const double PRIZE_1 = 500.00;
const double PRIZE_2 = 1000.00;
const double PRIZE_3 = 0.00;
const double PRIZE_4 = 10000.00;
const double PRIZE_5 = 0.00;
const double PRIZE_6 = 1000.00;
const double PRIZE_7 = 500.00;
const double PRIZE_8 = 100.00;

int main()
{
	
	int menuChoice;
	srand(42);
	
   cout << "Welcome to the plinko simulator!" << endl << endl;
   
	do
	{
		cout << "MENU: Please select one of the following options:" << endl << 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 << endl;
		cout << "Enter your selection now: " << endl;
		cin >> menuChoice;

		switch (menuChoice)
		{
		case 1:
			cout << "*** DROP SINGLE CHIP ***" << endl << endl;
			cout << "Which slot do you want to drop the chip in (0-8)? ";
			int slotNumber;
			cin >> slotNumber;
			if (slotNumber > 8 || slotNumber < 0)
				{
					cout << "INVALID SLOT. Please enter an integar between 0 and 8" << endl <<endl;
					break;
				}
			else
				{
					double slotNumberDouble = slotNumber;
					cout << fixed << setprecision(1);
					cout << endl;
					cout << "*** DROPPING CHIP INTO SLOT " << slotNumber << " ***";
					cout << endl;
					
					
					cout << "PATH: [" << slotNumberDouble;

					double chipLocation = slotNumber;
										
					for (int i = 0; i < 12; i++)
					{
						int randomNumber = rand();
						if (randomNumber % 2 == 1)
						{
							chipLocation += 0.5;
							if (chipLocation > 8)
							{
								chipLocation -= 1.0;
								cout << " " << chipLocation;
							}
							else
							{
								cout << " " << chipLocation;
							}
						}
						else
						{
							chipLocation -= 0.5;
							if (chipLocation < 0)
							{
								chipLocation += 1.0;
								cout << " " << chipLocation;
							}
							else
							{
								cout << " " << chipLocation;
							}
						}
					}

					cout << "]";
					
					int finalLocationChip2 = chipLocation;

					switch (finalLocationChip2)
					{
						case 0:
							finalLocationChip2 = PRIZE_0;
							break;
						case 1:
							finalLocationChip2 = PRIZE_1;
							break;
						case 2:
							finalLocationChip2 = PRIZE_2;
							break;
						case 3:
							finalLocationChip2 = PRIZE_3;
							break;
						case 4:
							finalLocationChip2 = PRIZE_4;
							break;
						case 5:
							finalLocationChip2 = PRIZE_5;
							break;
						case 6:
							finalLocationChip2 = PRIZE_6;
							break;
						case 7:
							finalLocationChip2 = PRIZE_7;
							break;
						case 8:
							finalLocationChip2 = PRIZE_8;
							break;
					}
					cout << fixed << setprecision(2) << "\nWINNINGS: $" << finalLocationChip2 << endl << endl;
				}
			break;

		case 2: 
		   cout << endl;
			cout << "*** DROP MULTIPLE CHIPS ***"<< endl;
			cout << "How many chips do you want to drop (>0)? ";
			int chipNumber;
			cin >> chipNumber;
			if (chipNumber < 1)
			{
				cout << "INVALID NUMBER OF CHIPS. Please enter an integar greater than zero." << endl << endl;
				break;
			}
			else
			{
				cout << "Which slot do you want to drop the chip in (0-8)? ";
				int slotNumber2;
				cin >> slotNumber2;
				if (slotNumber2 > 8 || slotNumber2 < 0)
				{
					cout << "INVALID SLOT. Please enter an integar between 0 and 8";
					cout<< endl << endl;
					break;
				}
				else
				{
					double totalWinnings = 0;
					for (int i = 0; i < chipNumber; i++)
					{
						double chipLocation = slotNumber2;

						for (int i = 0; i < 12; i++)
						{
							int randomNumber = rand();
							if (randomNumber % 2 == 1)
							{
								chipLocation += 0.5;
								if (chipLocation > 8)
								{
									chipLocation -= 1.0;
								}
							}
							else
							{
								chipLocation -= 0.5;
								if (chipLocation < 0)
								{
									chipLocation += 1.0;
								}
							}

						}
						int finalLocationChip2 = chipLocation;

						switch (finalLocationChip2)
						{
						case 0:
							finalLocationChip2 = PRIZE_0;
							break;
						case 1:
							finalLocationChip2 = PRIZE_1;
							break;
						case 2:
							finalLocationChip2 = PRIZE_2;
							break;
						case 3:
							finalLocationChip2 = PRIZE_3;
							break;
						case 4:
							finalLocationChip2 = PRIZE_4;
							break;
						case 5:
							finalLocationChip2 = PRIZE_5;
							break;
						case 6:
							finalLocationChip2 = PRIZE_6;
							break;
						case 7:
							finalLocationChip2 = PRIZE_7;
							break;
						case 8:
							finalLocationChip2 = PRIZE_8;
							break;
						}

						totalWinnings += finalLocationChip2;
					}
						cout << fixed << setprecision(2);
						cout << "Total winnings on " << chipNumber << " chips: $" << totalWinnings << endl;
						double averageWinningsPerChip = totalWinnings / chipNumber;
						cout << "Average winnings per chip: $" << averageWinningsPerChip << endl << endl;
				}
			}
				break;
		case 3: 
			cout << "GOODBYE!" << endl << endl;
			//system("pause");
			return 0;
			break;
		default:
			cout << "INVALID SELECTION.  Please enter 0, 1 or 2.\n\n";
			break;
		}
	} while (menuChoice >= 0 && menuChoice <0);
	//system("pause");
	return 0;
}
What part would you like us to debug?
What is your assignment?
I think my menuChoice while loop is wrong. it outpputs
my outoput
[Welcome to the plinko simulator!

MENU: Please select one of the following options:

0 - Quit the program
1 - Drop a single chip into one slot
2 - Drop multiple chips into one slot

Enter your selection now:
INVALID SELECTIO]

Expected output starts with

Welcome to the plinko simulator!

MENU: Please select one of the following options:

0 - Quit the program
1 - Drop a single chip into one slot
2 - Drop multiple chips into one slot

Enter your selection now:
GOODBYE!
One obvious problem is that at line 32, you say to enter 0 to quit. However, line 224 shows your exit code is option 3.

Line 25: srand(42) is always going to set the RNG to the same starting point. This may be useful for debugging.

Line 99-127, 184-214: These lines are identical. They should be placed in a function.
BTW, I absolutely detest nested switch statements.

Line 99-127, 184-214: You're using finalLocationChip2 for two different purposes. Location and prize amount. Not a good practice. It makes your program harder to debug. Better to use independent variables.

Last edited on
So how would i get my menu to come up again after an invalid selection? By the way, thanks so much!
Line 233: Take a look at your while condition. How can that statement EVER be true? menuChoice can not be >= 0 and < 0 and the same time. Therefore, your loop will exit after the first iteration.

Since you already have a way to exit the loop at line 224 and a way to handle invalid selections at line 229, I would simply change the condition to while (true)

THANK YOU! Ok, now how do I get my winnings to output with the .00
I tried doing fixed set precision but it still prints 1000 instead of 1000.00
You might want to check out some of the std::ios manipulators like std::ios::fixed and std::ios::showpoint.

http://www.cplusplus.com/reference/ios/fixed/
Topic archived. No new replies allowed.