1) Take a copy of the program below compile it and confirm it works for all numbers 1 - 12 input into the program.
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_mo nth()
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...
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
Code:
#include <stdio.h> 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 */
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_mo nth()
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...
Code:
int main(void){ int numeric_month; numeric_month = get_month(); print_normal_month(numeric_month); return 0; }
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
Comment