简体   繁体   中英

C program that calculates number of days between two dates

I have written a C program that calculates number of days between two dates. Unfortunately, it doesn't compile properly. I don't know why. Can someone please help me fix the code? It seems like there is some issue with SCANF and PRINTF functions. I don't even get a chance to input my own date.

This is the output that I get: Illegal date -1607965827

Please help me. Thanks in advance!

#include <stdio.h>
#include <stdlib.h>

int days_in_month[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};

struct date {
  int day;
  int month;
  int year;
};

int leap_year(int year) {
    if(year%400==0) return 1;

    if(year%4==0 && year%100!=0) return 1;

    return 0;
}

int correct(struct date d) {
    if(d.day < 1 || d.day > days_in_month[d.month]) return 0;

    if(d.month < 1 || d.month > 12) return 0;

    return 1;
}

int number_of_days(struct date d) {
    int result = 0;
    int i;

    for(i=1; i < d.year; i++) {
        if(leap_year(i))
            result += 366;
        else
            result += 365;
    }

    for(i=1; i < d.month; i++) {
        result += days_in_month[i];

        if(leap_year(d.year) && i == 2) result++;
    }

    result += d.day;
    return result;
}

int main() {
    struct date first, second;
    int days;

    scanf("%d %d %d", first.day, first.month, first.year);
    scanf("%d %d %d", second.day, second.month, second.year);

    if(!correct(first) || !correct(second)) {
        printf("Illegal date\n");
    }

    days = number_of_days(first) - number_of_days(second);

    printf("%d", days);

    return 0;
}

您需要使用传递给scanf的参数地址(使用&)

There are two issues:-
1. In main(), first.day is the name of the variable, rather we have to pass the reference(address) of the variable in the scanf("%d", &first.day);

2.Your days might be negative if(second date is greater than the first), you sure can fix that.

Incorrect usage of scanf() . %d expects a matching int * , not an int . Also, always check the return value of input functions. You will save time. Unless the return code is as expected, do not trust inputs like first.day to hopefully be given valid values. @MikeCAT

A good compile warns about mis-matched formats. Either enable all compiler warnings or get a new compiler.

// scanf("%d %d %d", first.day, first.month, first.year);
if (scanf("%d %d %d", &first.day, &first.month, &first.year) != 3) {
  puts("Bad input");
  return -1;
}

Other issues include:

days_in_month[d.month]) does not consider leap years. @BLUEPIXY
int main() not portable/correct - use int main(void)`.

Minor stuff:
leap_year() , issues with years before 1582. Could add year test in correct() . if (year > 1582) good();
int range may be insufficient for day number, use long .
for(i=1; i < d.year; i++) is inefficient.
Avoid magic numbers like i == 2
More common to pass structures by address than value.
"%d%d%d" will also work instead of "%d %d %d"

BYW: Nice use of structures.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM