easy question??

Hi all,

looking for some beginner tips, Im setting up a simple menu program using the switch/case statements, the menu choice will be an integer say 1, 2 or 3, I have a default case setup which will inform the user they must use 1,2 or 3 should they hit any other key, however if the user inputs a character rather than a integer the program crashes (obviously because the prog requests an int but gets a char), how can I have the program function accept either int or char? clear as mud??


Sample code below, if the user selects a char instead of an int the prog crashes, how do I prevent this??

1
2
3
4
5
6
7
8
9
10
11
int menu()
{
	int choice;
	cout << "(1) Test " << endl;
	cout << "(2) Redisplay menu " << endl;
	cout << "(3) Exit " << endl << endl;
	cout << ": ";
	cin >> choice;
	
	return choice;
}


Thanks.
change choice to either a char or string.

When you use choice the following table shows what you would compare choice to based on it's type.


type CompareTo
int 1
char '1'
string "1"
used char and it works fine, now embarassed at how simple the solution was, can you beleive I was looking at writing a function to convert char to an int!!!

thanks.
Topic archived. No new replies allowed.