Input error checking

This is a plinko simulator using functions. I am having trouble making my functions check for input error and reprompt. Also my allSlots function is not outputting what it needs to. Hopefully this makes sense.

(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", "Not even a number", and in the case of a slot number, invalid input like "9". You may assume that there will be no real-valued inputs like "2.3" Note that due to the improved error handling approach, some of the error messages have been modified.

Add a similar function for the menu too, reprompting if bad input is given.)

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

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!


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

using namespace std;

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;
	srand(42);
	
   cout << "Welcome to the plinko simulator!" << endl << endl;
   cout << fixed << setprecision(2);
   
	do
	{
		rootMenu();
		cin >> menuChoice;
		
		if (cin.fail()) {
		   cin.clear();
		   cin.ignore(1000, '\n');
		   cout << "INVALID SELECTION.  Please enter 0, 1, 2 or 3" << endl;
      }
		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);
			}
		}

		else if (menuChoice == 0)
		{
		   cout << "GOODBYE!";
			return 0;
		}
	}
	while (true);
	
	return 0;
}
I would tend to make getting and validating the input part of a single function. For example, I would have rootmenu() return only a valid choice.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//  Return only with a valid choice
int rootMenu() 
{   int choice;

    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;
	while (true)
	{   cin >> choice;
	    if (cin.fail()) 
	    {   cin.clear();
		    cin.ignore(1000, '\n');
		    cout << "INVALID SELECTION.  Please enter 0, 1, 2 or 3" << endl;
		    continue;
        }
        if (choice >= 0 && choice <= 3)
            return choice;  //  Input is good
    }            
}


Line 129: Calling srand(42) is going to start the RNG at the same place every time you run the program thereby always returning the same sequence of pseudo-random numbers. That's fine for debugging, but once you've debugged your program, you should do the following:
1
2
  
  srand(time(NULL));


Line 7: You don't need <stdlib.h>. You've already included <cstdlib>.

Line 118: You compute average, but don't do anything with it.



Topic archived. No new replies allowed.