Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write a program in C that prompts the user to enter two dates and then indicates which date comes earlier on the calender

Tags:

c

date

My code is as follow:

#include <stdio.h>
int main(void)
{
    int m1, m2, d1, d2, y1, y2;

    printf("enter first date (mm/dd/yy): ");
    scanf("%d/%d/%d", &m1, &d1, &y1);

    printf("enter second date (mm/dd/yy): ");
    scanf("%d/%d/%d", &m2, &d2, &y2);

    if (y1<y2)
        printf("%d/%d/%.2d is earlier than %d/%d/%.2d\n",m1, d1, y1, m2, d2, y2);
    else
        printf("%d/%d/%.2d is earlier than %d/%d/%.2d\n",m2, d2, y2, m1, d1, y1);

    if (y1==y2)
    {if (m1<m2)
            printf("%d/%d/%.2d is earlier than %d/%d/%.2d\n",m1, d1, y1, m2, d2, y2);
    else if (m2<m1)
        printf("%d/%d/%.2d is earlier than %d/%d/%.2d\n",m2, d2, y2, m1, d1, y1);}

    if (y1==y2 && m1==m2)
    {if (d1<d2)
        printf("%d/%d/%.2d is earlier than %d/%d/%.2d\n",m1, d1, y1, m2, d2, y2);
    else
        printf("%d/%d/%.2d is earlier than %d/%d/%.2d\n",m2, d2, y2, m1, d1, y1);}

    return 0;

}

Im trying to keep it simple since I am a beginner and am only on chapter 5 in King's C Programming Modern Approach Textbook. When I enter the following:

enter first date (mm/dd/yy): 03/31/93 enter second date (mm/dd/yy): 04/31/93

the response i get is:

4/31/93 is earlier than 3/31/93 AND 3/31/93 is earlier than 4/31/93

Any help is much appreciated. Thanks.

like image 513
Robro Avatar asked Mar 18 '23 15:03

Robro


2 Answers

Once you have found out that the year of the first date is less than the year of the second you should print an answer and return to prevent subsequent checks and result outputs, i.e.:

if (y1<y2) {
    printf("%d/%d/%.2d is earlier than %d/%d/%.2d\n",m1, d1, y1, m2, d2, y2);
    return 0;
}

Same applies to the month and the day field.

like image 70
Oleg Andriyanov Avatar answered Apr 26 '23 16:04

Oleg Andriyanov


In your first if's else block you don't check whether y1 is > than y2. That's why when they are equal (like in your case) y1 > y2 is false, you enter the else block and the first (incorrect) reply is printed.

One possible solution is to replace else with else if (like you did for month):

if (y1 < y2) {
    printf("%d/%d/%.2d is earlier than %d/%d/%.2d\n",m1, d1, y1, m2, d2, y2);
}
else if (y2 < y1) {
    printf("%d/%d/%.2d is earlier than %d/%d/%.2d\n",m2, d2, y2, m1, d1, y1);
}

When we fix that, the next problem is that if you enter two exactly the same dates, your code will output that the second one is earlier for the same reason as before: if (d1 < d2) will return false, and you will get to the else block. To fix that you could also use an else if:

if (y1 == y2 && m1 == m2) {
    if (d1 < d2) {
        printf("%d/%d/%.2d is earlier than %d/%d/%.2d\n",m1, d1, y1, m2, d2, y2);
    }
    else {
        if (d2 < d1) {
            printf("%d/%d/%.2d is earlier than %d/%d/%.2d\n",m2, d2, y2, m1, d1, y1);
        }
        else {
            printf("Dates are equal\n");
        }
    }
}

Also, to reduce code duplication, I would suggest to remember the result somehow, and then printf if only once in the end.

For instance, like so (one possible way):

#include <stdio.h>

#define FIRST_DATE_IS_BIGGER (1)
#define SECOND_DATE_IS_BIGGER (-1)
#define DATES_ARE_EQUAL 0

int main(void)
{
    int m1, m2, d1, d2, y1, y2;
    int result = DATES_ARE_EQUAL;

    printf("Enter the first date (mm/dd/yy): ");
    scanf("%d/%d/%d", &m1, &d1, &y1);

    printf("Enter the second date (mm/dd/yy): ");
    scanf("%d/%d/%d", &m2, &d2, &y2);

    if (y1 < y2) {
        result = SECOND_DATE_IS_BIGGER;
    }
    else if (y2 < y1) {
        result = FIRST_DATE_IS_BIGGER;
    }
    else {
        if (m1 < m2) {
            result = SECOND_DATE_IS_BIGGER;
        }
        else if (m2 < m1) {
            result = FIRST_DATE_IS_BIGGER;
        }
        else {
            if (d1 < d2) {
                result = SECOND_DATE_IS_BIGGER;
            }
            else if (d2 < d1) {
                result = FIRST_DATE_IS_BIGGER;
            }
        }
    }

    switch (result) {
        case FIRST_DATE_IS_BIGGER:
            printf("%d/%d/%.2d is earlier than %d/%d/%.2d\n",m2, d2, y2, m1, d1, y1);
            break;
        case SECOND_DATE_IS_BIGGER:
            printf("%d/%d/%.2d is earlier than %d/%d/%.2d\n",m1, d1, y1, m2, d2, y2);
            break;
        case DATES_ARE_EQUAL:
            printf("Dates are equal.\n");
            break;
    }
    return 0;
}

Ideone demo.

like image 42
FreeNickname Avatar answered Apr 26 '23 18:04

FreeNickname