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
+ 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 #include    int main(void){       int month;     printf("Month number (where January is 1): ");     scanf("%d", &month);     if(month == 1){         puts("January");     }else if(month == 2){         puts("February");     }else if(month == 3){         puts("March");     }else if(month == 4){         puts("April");     }else if(month == 5){         puts("May");     }else if(month == 6){         puts("June");     }else if(month == 7){         puts("July");     }else if(month == 8){         puts("August");     }else if(month == 9){         puts("September");     }else if(month == 10){         puts("October");     }else if(month == 11){         puts("November");     }else{         puts("December");     }     return 0; }   /*       filename: l9start.c   */ 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 int main(void){         int numeric_month;           numeric_month = get_month();         print_normal_month(numeric_month);           return 0;     } 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
3 Replies

 P: 7 The following is my solution for the C Programming problem above: Expand|Select|Wrap|Line Numbers #include    int main(void) {  int numeric_month;    numeric_month = get_month();    print_normal_month(numeric_month);    return 0; }   get_month() {  int month;      while (1)    {     printf("Month number (where January is 1): ");     scanf("%d", &month);        if (month > 0 && month <= 12)       {        return month;        break;       }     } }   print_normal_month(i) {  switch (i)    {    case 1: printf("January\n");    break;      case 2: printf("Feberuary\n");    break;      case 3: printf("March\n");    break;      case 4: printf("April\n");    break;      case 5: printf("May\n");    break;      case 6: printf("June\n");    break;      case 7: printf("July\n");    break;      case 8: printf("August\n");    break;      case 9: printf("September\n");    break;      case 10: printf("October\n");    break;      case 11: printf("November\n");    break;      case 12: printf("December\n");    break;   } } 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

 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   char line[100];   char *pEnd;     fgets(line, sizeof line, stdin);   month = strtol(line, &pEnd, 0);     /* Possibly additional code to check that pEnd now points to a string containing only whitespace or nothing to prevent inputs like "4T" slipping past */   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.