Plinko with Functions

I am having trouble completing this Plinko with functions lab. I have been working on this for a few days and feel completely stuck. The big problem that I'm running into is with the function for the menu, the other stuff is a problem too, but the menu feels like a good place to start? I can't seem to get it to "loop" through. I have been coding for just over a month now and would really appreciate some guidance on how I can complete this project.

I will include a background and the requirements below:

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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <limits>
using namespace std;

	double const SMALL_PRIZE = 100.00;
	double const MEDIUM_PRIZE = 500.00;
	double const LARGE_PRIZE = 1000.00;
	double const NO_PRIZE = 0;
	double const GRAND_PRIZE = 10000.00;

	int answer1 = 0;
	int answer2 = 0;
	int answer3 = 0;
	double slot;
	double bounce;
	int i = 0;
	int j = 0;
	const int RANDOM_SEED = 42;
	double moneyWon = 0.0;
	double totalMoney = 0.0;
	int menuChoice;
	int userSlotPos;

double ComputeWinnings(int slot) {

	double moneyWon;
	switch (static_cast<int>(slot)) {

		//Prize winnings!
	case 0:
		moneyWon = SMALL_PRIZE;
		break;

	case 1:
		moneyWon = MEDIUM_PRIZE;
		break;

	case 2:
		moneyWon = LARGE_PRIZE;
		break;

	case 3:
		moneyWon = NO_PRIZE;
		break;

	case 4:
		moneyWon = GRAND_PRIZE;
		break;

	case 5:
		moneyWon = NO_PRIZE;
		break;

	case 6:
		moneyWon = LARGE_PRIZE;
		break;

	case 7:
		moneyWon = MEDIUM_PRIZE;
		break;

	case 8:
		moneyWon = SMALL_PRIZE;
		break;
	}
	cout << fixed << setprecision(2) << "Winnings: $" << moneyWon << endl << endl;
	return moneyWon;
}

int GetSlotNumber() {
	int userSlotPos; //remember to cast to double. 

	cout << "Which slot do you want to drop the chip in (0-8)?" << endl;
	cin >> userSlotPos;

	while (cin.fail()) {

		cin.clear();
		cin.ignore(numeric_limits<streamsize>::max(), '\n');

		cout << "Try again" << endl;
		cin >> userSlotPos;
	}

	if (userSlotPos < 0 || userSlotPos > 8) {
		cout << "Invalid slot." << endl << endl;
	}
	else {
		return userSlotPos;
	}

}

double DropOneChip() {
	double slot;
	double slotShift;
	//int userSlotPos = 0;
	
	cout << "*** Drop a single chip ***" << endl << endl;
	GetSlotNumber();

		cout << "*** Dropping chip into slot " << userSlotPos << " ***" << endl;
		slot = static_cast<double>(userSlotPos);
		cout << fixed << setprecision(1) << "Path: [" << slot << ", ";
		for (int i = 0; i < 12; ++i) {
			if (slot == 0) {
				slot = slot + 0.5;
			}
			else if (slot == 8) {
				slot = slot - 0.5;
			}
			else if (slot > 0 && slot < 8) {
				slotShift = (rand() % 2) - 0.5;
				slot = slot + slotShift;
			}
			if (i == 11) {
				cout << fixed << setprecision(1) << slot << "]" << endl;
			}
			else {
				cout << fixed << setprecision(1) << slot << ", ";
			}
		}
	ComputeWinnings(slot);
	return slot;
}

//double DropMultipleChips() {
//	double slot;
//	double slotShift;
//	int userMultDrop;
//	int userSlotPos;
//	int i;
//	int j;
//
//	//GetSlotNumber();
//
//	for (j = 0; j < userMultDrop; ++j) {
//		slot = userSlotPos;
//		for (i = 0; i < 12; ++i) {
//			if (slot == 0) {
//				slot = slot + 0.5;
//			}
//			else if (slot == 8) {
//				slot = slot - 0.5;
//			}
//			else if (slot > 0 && slot < 8) {
//				slotShift = (rand() % 2) - 0.5;
//				slot = slot + slotShift;
//			}
//		}
//
//	}
//	ComputeWinnings(slot);
//}

int DisplayMenu() {
	int menuChoice;
	
	cout << "Enter your selection now:" << endl;
   cin >> menuChoice;
   
   if (menuChoice == 4) {
	cout << "Menu: Please select one of the following options:" << endl << endl;
	cout << "1 - Drop a single chip into one slot" << endl;
	cout << "2 - Drop multiple chips into one slot" << endl;
	cout << "3 - Drop multiple chips into each slot" << endl;
	cout << "4 - Show the options menu" << endl;
	cout << "5 - Quit the program" << endl;
   }


	//error checking false menu option choices
	while (menuChoice > 5 || menuChoice < 0) {
		cout << "Invalid selection.  Enter 4 to see options.";
	}

	//Quit, if they choose to
	if (menuChoice == 5) {
		cout << "Goodbye. " << endl;
	}
	return menuChoice;
}

int GetNumChips() {
	int numChips;

	cout << "*** Drop multiple chips ***" << endl << endl;
	cout << "How many chips do you want to drop (>0)?" << endl << endl;
	cin >> numChips;

	//if they don't have a number, reprompt
	while (cin.fail()) {

		cin.clear();
		cin.ignore(numeric_limits<streamsize>::max(), '\n');

		cout << "Try again" << endl;
		cin >> numChips;
	}

	//if they do an invalid number, let them know
	if (numChips <= 0) {
		cout << "Invalid number of chips.";
	}
	else {
		return numChips;
	}
}
int main() {
	//Declare variables
	int answer1 = 0;
	int answer2 = 0;
	int answer3 = 0;
	double slot;
	double bounce;
	int i = 0;
	int j = 0;
	const int RANDOM_SEED = 42;
	double moneyWon = 0.0;
	double totalMoney = 0.0;
	int menuChoice;
	int userSlotPos;

	srand(RANDOM_SEED);

	//DisplayMenu();
	
	cout << "Welcome to the Plinko simulator! Enter 4 to see options." << endl << endl;
	
	menuChoice = DisplayMenu();
	
	switch (menuChoice) {
		case 1://1 - Drop a single chip into one slot
			DropOneChip();
			break;
		case 2://2 - Drop multiple chips into one slot
			//DropMultChips();
			break;
		case 3://3 - Drop multiple chips into each slot
         break;
		case 4: //4 - Show the options menu
			DisplayMenu();
			break;
		case 5: //5 - Quit the program
			return 0;
			break;
	}


	cout << menuChoice;


	//system("pause");
	return 0;

}



Here is some background about the project and the requirements:

Part 1 - Refactoring (35 points)

In particular we are expecting to see functions for:
Computing the amount of prize money won for a single chip given the slot the chip ended up in-- this function must be defined as "double ComputeWinnings(int slot) {… your code here …}. (See the original Plinko lab for the complete list of these values).
Simulating one chip falling-- you will have to use your discretion on how you name and code this, but it should make both the "Drop a single chip into one slot" option and the next function (Simulating multiple chips falling) easier to write. This function must make use of the "ComputeWinnings" function described above.
Simulating multiple chips falling-- similarly this function should make both the "Drop multiple chips into one slot" option and a new "Drop multiple chips into each slot" option (described below) easier to write. This function must make use of the "Simulating one chip falling" function described above.
Add a new menu item for "Drop multiple chips into each slot", but you do not have to implement it yet. You will implement it in Part 3.

Part 2 - Better Error Handling (30 points)

Add a function for getting the number of chips and a function for getting the slot number. Within these functions check for user input error and reprompt until the user provides valid input. You must be able to recognize erroneous input like "-1", "x", "Not even a number", and in the case of a slot number, invalid integers like "9". You may assume that there will be no real-valued inputs like "2.3". Add a function for getting the menu option also, reprompting if an erroneous option is input.

If you think about it for a minute you will realize that you could use a single function rather than three separate functions for inputting a correct integer. Obviously it would need additional parameters to work as required in each case, but it is a better approach. We encourage you to do it with just one function. More general functions like this have the advantage of being re-usable in later programs which need an integer input, rather than having to write a new function for each specific variation on inputting an integer.

Part 3 - Functions and the Power of Code Reuse (20 points)

In this part of the lab, you will take advantage of functions to add an additional menu option to your Plinko program, without having to duplicate code from the other menu options.

Add a new menu option 3 that allows the user to drop the same number of chips into each of the 9 slots. For example, if the user enters 5, you simulate dropping 5 chips into each of the 9 slots.

Prompt the user to enter one number, which is the number of chips to drop into every slot. If the number of chips is non-positive or a string, etc., reprompt until you get valid input (hint: just use the function you wrote in Part 2)

For each slot, report the total and average amount of money won for all chips dropped into that slot.

Deductions
New functions for:

Computing the amount of prize money won for one chip given the slot it ended up in (called "ComputeWinnings"). (5 points)

Simulating one chip falling (must use "ComputeWinnings"). (5 points)

Simulating multiple chips falling (must use "Simulate one chip falling"). (5 points)

New functions for menu, slot and chip input, with error handling. (5 points) It could be a single function with additional parameters as noted.

Notes

The test cases start with just the input part, so you can start there and then build your solution step by part.
Well, there's nothing in your DisplayMenu() or main() functions that does any looping, so we can't see what you've tried, which means that we can't help you understand what you did wrong, or how to fix it.

When creating a loop, you need to think clearly about three things:

1) What operations do you want to be repeated on each iteration of the loop?
2) What determines how many times the loop keeps iterating? In other words, what condition(s) do you want to cause the loop to stop iterating?
3) What conditions need to be set up at the start of the loop?

Once you've figured out those things, then it should be straightforward to figure out what code you need to write to make it happen.
Topic archived. No new replies allowed.