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

How to only limit numerical values in int for C Programming.

P: 7
1) Take a copy of the program below compile it and confirm it works for all numbers 1 - 12 input into the program.

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. int main(void){
  4.  
  5.     int month;
  6.     printf("Month number (where January is 1): ");
  7.     scanf("%d", &month);
  8.     if(month == 1){
  9.         puts("January");
  10.     }else if(month == 2){
  11.         puts("February");
  12.     }else if(month == 3){
  13.         puts("March");
  14.     }else if(month == 4){
  15.         puts("April");
  16.     }else if(month == 5){
  17.         puts("May");
  18.     }else if(month == 6){
  19.         puts("June");
  20.     }else if(month == 7){
  21.         puts("July");
  22.     }else if(month == 8){
  23.         puts("August");
  24.     }else if(month == 9){
  25.         puts("September");
  26.     }else if(month == 10){
  27.         puts("October");
  28.     }else if(month == 11){
  29.         puts("November");
  30.     }else{
  31.         puts("December");
  32.     }
  33.     return 0;
  34. }
  35.  
  36. /*
  37.  
  38.     filename: l9start.c
  39.  
  40. */
2) You are going to modify this program. You will write two functions which will be called from the main function.
3) The first function will be called get_month()
FEATURES:
a) The function will return an integer.
b) The function does not accept any input when it is called.
c) In this function declare a local variable named month as an integer.
d) Validate the data entered by the user. It has to be a number from 1 to 12. Discard rubbish if the user types some non-numeric characters.
e) The return value is the valid numberic month entered by the user.
4) The second function will be called print_normal_month()
FEATURES:
a) Pass the data entered by the user into this function when you call it from the main function.
b) This function does not return any data.
c) The if-else decision structure given in the starter code has to go into this function. Determine what month it is and print the appropriate string.
d) Get your new program to work correctly before proceding to the next step.
e) The last thing to do is replace the if-else structure with a switch-cse structure.
5) The main part of your new program will look like...
Expand|Select|Wrap|Line Numbers
  1. int main(void){
  2.         int numeric_month;
  3.  
  4.         numeric_month = get_month();
  5.         print_normal_month(numeric_month);
  6.  
  7.         return 0;
  8.     }
Sample Output
lab9.c
Month number (where January is 1): 13
Month number (where January is 1): 0
Month number (where January is 1): al;skdjf
Month number (where January is 1): 4
April

lab9.c
Month number (where January is 1): 11
November

lab9.c
Month number (where January is 1): 1
January
Nov 1 '06 #1
Share this Question
Share on Google+
3 Replies


P: 7
The following is my solution for the C Programming problem above:
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. int main(void)
  4. {
  5.  int numeric_month;
  6.  
  7.  numeric_month = get_month();
  8.  
  9.  print_normal_month(numeric_month);
  10.  
  11.  return 0;
  12. }
  13.  
  14. get_month()
  15. {
  16.  int month; 
  17.  
  18.   while (1)
  19.    {
  20.     printf("Month number (where January is 1): ");
  21.     scanf("%d", &month);
  22.  
  23.      if (month > 0 && month <= 12)
  24.       {
  25.        return month;
  26.        break;
  27.       } 
  28.    }
  29. }
  30.  
  31. print_normal_month(i)
  32. {
  33.  switch (i) 
  34.   {
  35.    case 1: printf("January\n");
  36.    break;
  37.  
  38.    case 2: printf("Feberuary\n");
  39.    break;
  40.  
  41.    case 3: printf("March\n");
  42.    break;
  43.  
  44.    case 4: printf("April\n");
  45.    break;
  46.  
  47.    case 5: printf("May\n");
  48.    break;
  49.  
  50.    case 6: printf("June\n");
  51.    break;
  52.  
  53.    case 7: printf("July\n");
  54.    break;
  55.  
  56.    case 8: printf("August\n");
  57.    break;
  58.  
  59.    case 9: printf("September\n");
  60.    break;
  61.  
  62.    case 10: printf("October\n");
  63.    break;
  64.  
  65.    case 11: printf("November\n");
  66.    break;
  67.  
  68.    case 12: printf("December\n");
  69.    break;
  70.   }
  71. }
The only problem that I have is that when I enter a non-numeric character (eg: al;skdjf) the program crashes. Any ideas on how I can tell the program to only accept values between 1 - 12 and disregard everything else without crashing the program?
Nov 1 '06 #2

Banfa
Expert Mod 5K+
P: 8,916
The problem is this line

scanf("%d", &month);

You are telling the program to get an integer so things go wrong when you enter letters.

scanf is unsafe anyway you need to replace it with

Expand|Select|Wrap|Line Numbers
  1.   char line[100];
  2.   char *pEnd;
  3.  
  4.   fgets(line, sizeof line, stdin);
  5.   month = strtol(line, &pEnd, 0);
  6.  
  7.   /* Possibly additional code to check that pEnd now points to a string containing only whitespace or nothing to prevent inputs like "4T" slipping past */
  8.  
Nov 1 '06 #3

P: 7
Yup it works out perfect....Thanks for the help.
Nov 2 '06 #4

Post your reply

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