How do I limit user input in Console applications

Hi, i'm in a small programming contest at school, and one of the big factors in judging the programs is going to be how well it handles input errors. I had an idea that would look really good to judges-- Is there a way to limit input from the user such as them only being able to enter numbers, or only the alphabet, maybe even two certain letters like (Y/N). If there isn't a way to do that, i'd really appreciate any other ideas to handle input error besides:

(The menu has 5 choices)

if(MenuChoice < 1 || MenuChoice > 5)
{
cout << "Please try again" << endl;
}

I may have explained a little vaguely but I didn't know how to put it any other way...
Last edited on
Well, you could use a try-catch system: http://www.cprogramming.com/tutorial/exceptions.html

And you could design special functions to ask the user for input from a certain type and with certain limits (eg: int inputInt(int min, int max);) that validate the input and keep asking when the input is invalid. I would use do-while loops.
Last edited on
Exceptions shouldn't be used to control flow.

Read this instead:
http://www.cplusplus.com/forum/articles/6046/
Last edited on
Zaita: Seconded. Never use exceptions in a flow that can be considered part of normal execution.
Topic archived. No new replies allowed.