Functions!

Hey so we are making a program and have to run it using srand(42). I am having trouble making my input repromt when they enter invalid input and not repromt the whole menu but just the selection.

Expected Output:
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
3 - Drop multiple chips into each slot

Enter your selection now:
INVALID SELECTION. Please enter 0, 1, 2 or 3
Enter your selection now:
INVALID SELECTION. Please enter 0, 1, 2 or 3
Enter your selection now:
INVALID SELECTION. Please enter 0, 1, 2 or 3
Enter your selection now:
INVALID SELECTION. Please enter 0, 1, 2 or 3
Enter your selection now:
GOODBYE!

my output:
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
3 - Drop multiple chips into each slot

Enter your selection now:
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
3 - Drop multiple chips into each slot

Enter your selection now:
INVALID SELECTION. Please enter 0, 1, 2 or 3
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
3 - Drop multiple chips into each slot

Enter your selection now:
MENU: Please select one o

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

using namespace std;

bool menuError(int menuChoice)
{
	if (menuChoice > 3 || menuChoice < 0)
	{
		cout << endl << "INVALID SELECTION.  Please enter 0, 1, 2, or 3" << endl << endl;

		return true;
	}

	else
	{
		return false;
	}
}
void rootMenu() {
      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;
		cout << "3 - Drop multiple chips into each slot" << endl << endl;
		cout << "Enter your selection now: " << endl;
}

double ComputeWinnings(double location)
{
	double prize;
	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;

	if (location == 0) { prize = PRIZE_0; }
	else if (location == 1) { prize = PRIZE_1; }
	else if (location == 2) { prize = PRIZE_2; }
	else if (location == 3) { prize = PRIZE_3; }
	else if (location == 4) { prize = PRIZE_4; }
	else if (location == 5) { prize = PRIZE_5; }
	else if (location == 6) { prize = PRIZE_6; }
	else if (location == 7) { prize = PRIZE_7; }
	else if (location == 8) { prize = PRIZE_8; }

	return prize;

}

double singleChip(int slotNumber, int chipNumber)
{
	double location = slotNumber;

	if (slotNumber >= 0 && slotNumber <= 8) 
	{
		for (int i = 0; i < 12; i++) 
		{

			double random = rand() % 2;
			if (random < 1)
			{
				location = location - 0.5;
			}
			else
			{
				location = location + 0.5;
			}
			if (location < 0)
			{
				location = 0.5;
			}
			else if (location > 8)
			{
				location = 7.5;
			}
		}


	}


	return location;
}

double multipleChips(int slotNumber, int chipNumber){
   double totalWinnings = 0;
	double location = slotNumber;

	if (chipNumber > 0)
	{
		if (slotNumber >= 0 && slotNumber <= 8)
		{

			for (int i = 0; i < chipNumber; i++)
			{ 
				location = singleChip(slotNumber, chipNumber);
				totalWinnings = totalWinnings + ComputeWinnings(location);
				//cout << location;
			}

		}
	}
   double average = totalWinnings / chipNumber;
	cout << "Total Winnings on " << chipNumber << " chips: " << totalWinnings << endl;
   cout << "Average winnings per chip: " << average << endl << endl;
	return location;
}

double allSlots(int slotNumber, int chipNumber)
{
	double location = slotNumber;
	double totalWinnings = 0;

	for (int i = 0; i <= 8; i++)
	{
		slotNumber = i;
		location = multipleChips(slotNumber, chipNumber);
		totalWinnings = totalWinnings + ComputeWinnings(location);

	}
	double average = totalWinnings / chipNumber;

	return location;
}
	
int main()
{
	double location = 0;
	int slotNumber = 0;
	int chipNumber = 0;
	int menuChoice;
	bool quit = false;
	srand(42);
	
   cout << "Welcome to the plinko simulator!" << endl << endl;
   cout << fixed << setprecision(2);
   do {
	   do {
		rootMenu();
		cin >> menuChoice;
      }
      while (menuError(menuChoice));
      if (menuChoice == 0)
		{
			cout << "GOODBYE!";
			quit = true;
		}
		else if (menuChoice == 1){
		  cout <<"*** DROP SINGLE CHIP ***" << endl << endl;
			chipNumber = 1;
			cout << "Which slot do you want to drop the chip(s) in (0-8)? " << endl;
			slotNumber = 0;
			cin >> slotNumber;

			if (slotNumber >= 0 && slotNumber <= 8)
			{
				location = singleChip(slotNumber, chipNumber);
				cout << "WINNINGS: $" << ComputeWinnings(location) << endl << endl;
			}
		}

		else if (menuChoice == 2)
		{
		   double totalWinnings = 0;
		   cout << "*** DROP MULTIPLE CHIPS ***" << endl << endl;
			cout << "How many chips do you want to drop (>0)? " << endl;
			cin >> chipNumber;
			if (chipNumber > 0)
			{
				cout << "Which slot do you want to drop the chip(s) in (0-8)? " << endl;
				slotNumber = 0;
				cin >> slotNumber;

				if (slotNumber >= 0 && slotNumber <= 8) //check this part again
				{
					location = multipleChips(slotNumber, chipNumber);
				}
			}
		}

		else if (menuChoice == 3)
		{
			cout << "How many chips do you want to drop (>0)? " << endl;
			cin >> chipNumber;
			cout << "*** SEQUENTIALLY DROP MULTIPLE CHIPS ***" << endl << endl;

			if (chipNumber > 0)
			{
				location = allSlots(slotNumber, chipNumber);
			}
		}

	}
	while (quit == false);
	
	return 0;
}
Last edited on
Topic archived. No new replies allowed.