I am trying to add code to check if the month
input on the command line is in the range 1 to 12.
If it is not then print out the following error message and exit the program
(Make sure that your return code is non-zero.)
$ ./dates 1 13 2019
Error -the month entered (13) is not in the proper range (1-12)
#include <stdio.h>
#include <stdlib.h>
int main ( int argc, char *argv[] ) {
/* Names of the months */
char *monthName[12] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
/* The number of days in each month */
int monthLength[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int dd = 0;
int mm = 0;
int yyyy = 0;
if ( argc < 4 ) {
printf ( "Usage: ./dates mm dd yyyy \n" );
exit ( 1 );
} else {
dd = atoi ( argv[1] );
mm = atoi ( argv[2] );
yyyy = atoi ( argv[3] );
}
if (1<=mm<=12) {
printf ( "The date is %s %02d, %04d\n", monthName[mm-1], mm, yyyy);
} else {
printf ("Error - the month enteres (%d) is not in the proper range ( 1-12)", mm);
}
return (1) ;
}