Switch function

How do I get the switch function's default action to reject characters (A,B,C) as well as numbers that do not fit into its cases.

Currently i can only get it to work with numbers not in the cases.

If there is a way to do this with another type of loop i would like to know.
Last edited on
Well you can use isalpha() for the letters and reject it if the function returns true. You could make your own function with if else if else statements for the nonconforming numbers.

edit: whoah bad suggestion, your switch case would work great if you only want like 8 numbers or something... in the default just return like -1 or something if it's in a function otherwise simply set some value to false and don't do the next step if that bool is false, you jelly?
Last edited on
default is the label reached if none of the case lables were reached.
Last edited on
The problem is when i enter something like a letter or letters it will end the while loop and print the last cout. I want it to reloop if the options are not 1 or 2 .




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
int code;
int choice;
	
	


while  (cin>> choice)
{

	switch ( choice )
	{
	case 1: 
		cout << "Enter a 4-digit value to encrypt:";
		cin >> code;
		encryptCode.setCode( code );
		encryptCode.displayMessage();
		break;

	case 2:
		cout << "Enter a 4-digit value to decrypt:";
		cin >> code;
		decryptCode.setCode( code );
		decryptCode.displayMessage();
		break;

	
	default:
		cout << " Try again";
	}
	
}

	cout << "Exiting" << std::endl;
	
	


	



Last edited on
That's because cin >> choice will fail on non-numeric input. See http://www.cplusplus.com/forum/articles/6046/
Topic archived. No new replies allowed.