It usually takes me at least an hour to do the last problem my instructor assigns, but I got done with this one in 15 min. Since it is an online course I can't directly ask my instructor if I am doing it properly (at least I thnk I can't) and he would have to be online to check it before the deadline. So I was hoping you guys could give me second opinion on if this is correct.
Here is the problem:
Write and run a C program that requests both a month and a day value. Only month values between 1 and 12 should be accepted. Further, day values between 1 and 28 should be accepted for month 2 (Februrary); day values between 1 and 30 should only be accepted for months 4, 6, 9, and 11 (April, June, Spetember, and November); and only day values between 1 and 31 should be accepted for all remaning months. (Hint: Use a switch statement)
Here is the code:
Code:
//Cameron Tayor
#include <stdio.h>
#define MAXCOUNT 10
int main (){
int opselect;
int day, month;
printf("Enter a number for the month");
scanf("%d", &month);
printf("Enter a number for the day of the month");
scanf("%d", &day);
printf("Enter a seclect code");
printf("\n 1 for months 1-4");
printf("\n 2 for months 5-8");
printf("\n 3 for months 9-12");
scanf("%d", &opselect);
switch (opselect) {
case 1:
if (month == 1 && day > 0 && day < 32)
printf("The date you have entered was January %d", day);
else if (month == 2 && day > 0 && day < 29)
printf("The date you have entered was February %d", day);
else if (month == 3 && day > 0 && day < 32)
printf("The date you have entered was March %d", day);
else if (month == 4 && day > 0 && day < 31)
printf("The date you have entered was April %d", day);
else
printf("The date you entered is not valid");
break;
case 2:
if (month == 5 && day > 0 && day < 32)
printf("The date you have entered was May %d", day);
else if (month == 6 && day > 0 && day < 31)
printf("The date you have entered was June %d", day);
else if (month == 7 && day > 0 && day < 32)
printf("The date you have entered was July %d", day);
else if (month == 8 && day > 0 && day < 32)
printf("The date you have entered was August %d", day);
else
printf("The date you entered is not valid");
break;
case 3:
if (month == 9 && day > 0 && day < 31)
printf("The date you have entered was September %d", day);
else if (month == 10 && day > 0 && day < 32)
printf("The date you have entered was October %d", day);
else if (month == 11 && day > 0 && day < 31)
printf("The date you have entered was November %d", day);
else if (month == 12 && day > 0 && day < 32)
printf("The date you have entered was December %d", day);
else
printf("The date you entered is not valid");
break; }
return 0;
}
It works just fine, but I don't want to be too secure if I might of missed something. The chapter we are doing just went finished going over for and while loops. So it doesn't fit with the chapter, but asks specifically for a switch statement.