So I just started a "C" class and I've done my first few assignments but I feel like I'm over complicating the codes by using to many if else statements. Here's the assignment and my code. Please let me know what you think:
Write a program that asks the user to enter two dates and then indicates which date comes earlier on the calendar.
Your output should match the example below (except that the input will be different for other executions!) User input is underlined.
Enter first date (mm/dd/yyyy): 3/6/2008
Enter the second date (mm/dd/yyyy): 5/17/2007
5/17/2007 is earlier than 3/6/2008
/*
Program assignment name: 2_Dates
Author:Christopher D*****
*/
#include <stdio.h>
int main (void)
{
int mm, dd, yyyy, mm2, dd2, yyyy2;
printf("Enter first date (mm/dd/yyyy): ");
scanf("%d/%d/%d", &mm, &dd, &yyyy);
printf("Enter second date (mm/dd/yyyy): ");
scanf("%d/%d/%d", &mm2, &dd2, &yyyy2);
if (yyyy < yyyy2)
{
printf("%2d/%2d/%4d is earlier than %2d/%2d/%4d\n", mm, dd, yyyy, mm2, dd2, yyyy2);
}
else if (yyyy > yyyy2)
{
printf("%2d/%2d/%4d is earlier than %2d/%2d/%4d\n", mm2, dd2, yyyy2, mm, dd, yyyy);
}
else if (yyyy == yyyy2 && mm < mm2)
{
printf("%2d/%2d/%4d is earlier than %2d/%2d/%4d\n", mm, dd, yyyy, mm2, dd2, yyyy2);
}
else if (yyyy == yyyy2 && mm > mm2)
{
printf("%2d/%2d/%4d is earlier than %2d/%2d/%4d\n", mm2, dd2, yyyy2, mm, dd, yyyy);
}
else if (dd < dd2)
{
printf("%2d/%2d/%4d is earlier than %2d/%2d/%4d\n", mm, dd, yyyy, mm2, dd2, yyyy2);
}
else if (dd2 > dd)
{
printf("%2d/%2d/%4d is earlier than %2d/%2d/%4d\n", mm2, dd2, yyyy2, mm, dd, yyyy);
}
else
{
printf("The dates you entered are the same\n");
}
return 0;
}