#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    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...
  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    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.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0
    so
    is there any code to create this program: D
    please in share here
  6. #4
  7. Hats off to Mr. Joseph donahue
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2009
    Posts
    752
    Rep Power
    1107
    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.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0
    could help in the share code here
  10. #6
  11. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    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

    • ptr2void agrees

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  12. #7
  13. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,253
    Rep Power
    2222
    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 03:35 PM.

IMN logo majestic logo threadwatch logo seochat tools logo