algorithm - C program that calculates number of days between two dates -
i have written c program calculates number of days between 2 dates. unfortunately, doesn't compile properly. don't know why. can please me fix code? seems there issue scanf , printf functions. don't chance input own date.
this output get: illegal date -1607965827
please me. 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; < d.year; i++) { if(leap_year(i)) result += 366; else result += 365; } for(i=1; < d.month; i++) { result += days_in_month[i]; if(leap_year(d.year) && == 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; }
you need use addresses of arguments passed scanf (use &)
Comments
Post a Comment