Code:
/*
Namme: Connor Moore
Date: 1/29/2013
File: compareNDates.c
Purpose: This program prompts a user to enter
any number of dates until the user enters 0/0/0.
The program will then print out the date that is
the earliest.
*/
#include <stdio.h>
void compare();
int main (void) {
int mm = 1, dd = 1, yyyy = 1, mm2 = 0, dd2 = 0, yyyy2 = 0;
/* Prompt for and get the first date*/
printf("Enter first date (mm/dd/yyyy): ");
scanf("%d /%d /%d", &mm, &dd, &yyyy);
}
/* Prompt for and get the second date*/
printf("Enter second date (mm/dd/yyyy): ");
scanf("%d /%d /%d", &mm2, &dd2, &yyyy2);
/*Function Call*/
compare(mm, mm2, dd, dd2, yyyy, yyyy2);
return 0;
}
/*Function that takes in each section of the dates
entered by the user and compares them starting
with the years, then the months, then the days.
After comparison it prints out which is earlier.
Also added in what to do in case a user enters
the same dates.*/
void compare(int mm, int mm2, int dd, int dd2,
int yyyy,int 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 (mm < mm2) {
printf("%2d/%2d/%4d is earlier than %2d/%2d/%4d\n",
mm, dd, yyyy, mm2, dd2, yyyy2);
}
else if (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");
}
}
}
}