avoid switch crash

Hey folks,

I've already turned in this assignment as it fulfills the requirements. I was just wondering if anyone knew how to make it so that users can input a string without making my switch crash (go into continuous looping). Preferably if they enter a letter/string I would like the program to cout "That is not a valid option" and then display the menu again.

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
#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

//My menu function-
void displayMenu(int &option)
{
    cout << "Select an option: "<< endl;
    cout << "1 - to drop one chip into one slot" << endl;
    cout << "2 - to drop multiple chips into one slot" << endl;
    cout << "3 - to quit\n" << endl;
    cin >> option;
}

//My plinko board function to be called on later-
int plinkodrop(int slot, int showslots)
{
	double location = slot;
	for(int i = 0; i < 12; i++)
	{
		double random = (rand() %10);
		if(random < 5)
		{
			location -= 0.5;
		}
		else
		{
			location += 0.5;
		}
		if(location < 0)
		{
			location = 0.5;
		}
		else if(location > 8)
		{
			location = 7.5;
		}

		if(showslots == 1)
		{
			cout << location << endl;
		}
	}

	int prize = 0;
	if(location == 0){prize = 100;}
	else if(location == 1){prize = 500;}
	else if(location == 2){prize = 1000;}
	else if(location == 3){prize = 0;}
	else if(location == 4){prize = 10000;}
	else if(location == 5){prize = 0;}
	else if(location == 6){prize = 1000;}
	else if(location == 7){prize = 500;}
	else if(location == 8){prize = 100;}

	return prize;
}


int main()
{
    int option = 0;
	int slot = 0;
	int chips = 0;

//I chose to use the bool statement followed by a switch for the menu-
	bool exit = false;
    
	while(!exit)
	{
        displayMenu(option);
		switch(option)
		{
			case 1:
				cout << "\nYou want to drop a chip into a slot.\n"
					<< "There are 9 slots (0-8),\n"
					<< "Which slot would you like?: \n"
					<< "Slot: ";
				cin >> slot;
				if((slot >= 0) && (slot <= 8))
				{
					int prize = plinkodrop(slot, 1);
					cout << "\nYou won $" << prize << "\n" << endl;
				}
				else
				{
					cout << "\n";
					break;
				}
				break;
			
			case 2:
				cout << "\nYou want to drop multiple chips.\n"
					<< "How many chips would you like to drop?\n"
					<< "Chips: ";
				cin >> chips;
				if(chips < 0)
				{
					cout << "\n";
					break;
				}
				else
				{
					cout << "\nThere are 9 slots (0-8),\n"
					<< "Which slot would you like to drop your chips in?\n"
					<< "Slot: ";
					cin >> slot;
					if((slot < 0) || (slot > 8))
					{
						cout << "\n";
						break;
					}
					else
					{
						double totalprize = 0;
						for(int i = 1; i <= chips; i++)
						{
							totalprize += plinkodrop(slot, 0);
						}
						double average = totalprize / chips;
						cout << "Average prize money per chip: $" << average << endl;
						cout << "Total prize money: $" << totalprize << "\n" << endl;
					}
				}
				break;
			
			case 3:
				exit = true;
				break;
			default:
				cout << "\nIncorrect selection. Try again.\n" << endl;
				break;
		}
	}
	cout << "Exiting Program..." << endl;
	cin.ignore();
	cin.get();
	return 0;
}
Last edited on
use isalpha() from cctype
For example function to validate input:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <limits>
#include <string>

template <typename T>
void validated_input(T& value, std::string message = "invalid value\n")
{
    while(!(std::cin >> value)) {
        std::cout << message;
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }
}
//usage:
int x;
validated_input(x);
//or
validated_input(x, "Enter number");
Topic archived. No new replies allowed.