April 5th, 2011, 09:14 AM
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...
April 5th, 2011, 09:44 AM
You pretty much are stuck with your example. Even with GUIs you are doing the exact same thing, just with character-by-character effort (which, btw, you can do with console applications if you want to do so). Until the program has the data it can't evaluate it, so your program has to read everything.
April 5th, 2011, 10:09 AM
is there any code to create this program: D
please in share here
April 5th, 2011, 10:17 AM
The most easiest way that I know of would be to:
1. Take the input from the user
2. Validate the input.
3. If input invalid go to step 1
4. do whatever you want with the input
Preferably use a do-while loop to do so.
April 5th, 2011, 10:55 AM
could help in the share code here
April 5th, 2011, 12:03 PM
If you haven't figured it out already, we don't help those who won't make any effort to help themselves. If you want to give it a try and then ask for help with specific issues we are happy to help, otherwise you will find few friends here.
Comments on this post
April 5th, 2011, 02:27 PM
Teaching you to fish, instead of just giving you a fish, would be helping you.
This index page points to a Windows Consoles tutorial: http://www.adrianxw.dk/SoftwareSite/index.html Part 5 of the tutorial deals with keyboard and mouse events.
BTW, a partial implementation of that old conio library made use of the Console API.
Or a simple menu as RAJ described. A do-while loop accepting input from the user and validating it. Straight-forward stuff. First month or two of beginning class, easily.
Last edited by dwise1_aol; April 5th, 2011 at 02:35 PM.