By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,285 Members | 2,147 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,285 IT Pros & Developers. It's quick & easy.

checking that user input certain integer

P: 1
Hi all,

I am trying to display a menu of choices in my c program, and my program will return an error if user do not enter the right integer or entered a non-integer input. I tried:

Expand|Select|Wrap|Line Numbers
  1. printf("Enter option:");
  2. scanf("%d%c", &option, &term);
  3. //check for error if option is lower than 0 or more than 10
  5. while((term!='\n')||((option<0)||(option>10)))
  6. {
  7. printf("Wrong Input, please try again.\n");
  8. printf("Enter option:");
  9. scanf("%d%c", &option, &term);
  10. }
please help thanks :(
Sep 15 '11 #1
Share this Question
Share on Google+
2 Replies

Expert Mod 5K+
P: 8,916
You haven't said how this behaves and in what way that behaviour is undesirable.
Sep 16 '11 #2

P: 2
Switch case might help you.Here is a simple syntax which will solve your problem.
Expand|Select|Wrap|Line Numbers
  1. Switch(choice)
  2. {
  3. case 1:
  4. Your first option
  5. case 2:
  6. Your second option
  8. //and so on,depends upon no. of options you have in your menu.
  9. //Then finally
  11. default:
  12. printf("invalid input.please try again");
  14. //if you enter numbers other than the ones following the switch cases,switch case will execute the default statement.
  16. }
Sep 18 '11 #3

Post your reply

Sign in to post your reply or Sign up for a free account.