I am trying to write a function that allows a user to enter an option from a menu. The menu options are numbered from 1 to 9.
I quickly found out that if I just did a cin>> with an int variable that the program would crash it a non number character was entered ( ie a through z ). So now I am trying to make the function so that it requests a char var. instead of a int type, then looks at the value entered. If it is an integer value it will return that number. If it is anything else it will display a warning and retry. Only thing is that I am stuck on how to determine if the value entered is an integer or a character. Here is what I have so far....
or is there just a better way?Code:int getselection() { char selection; int x=0; do { cin>> selection; if ( /* if selection is an integer */ ) { x=1; } else { cout<<"incorrect selection!"; } } while ( x==0 ); return atoi(selection); }
Using DEV-C++ under Windows XP