Help completing a code

Problem: Write and execute a program to accept two dates in the format dd/mm/yyyy and print the number of days between the two dates if both the dates are valid. - C language


I know the general formula for the code I'm just not sure what I should make for the statements to execute it properly.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>
    int dateSerial (int ,int, int );

int main(void) {

int m,d,y;
printf("Enter one date in the format of dd/mm/yyyy : ");
scanf("%d %d %d",&d,&m,&y);

printf("Enter another date in the format of dd/mm/yyyy : ");
scanf("%d %d %d",&d,&m,&y);

m = (m + 9) % 12;
y = y - m/10;
return 365*y + y/4 - y/100 + y/400 + (m*306 + 5)/10 + (d-1);
}

Last edited on
Why do you return a complicated value in the function main? It does nothing.
return 365*y + y/4 - y/100 + y/400 + (m*306 + 5)/10 + (d-1);
That is not a complicated value. That is an integer. The shell that does invoke this program can use the returned value.

However, it is recommended to return only small non-negative integers; some systems only a limited number of bits.


1. Check that both dates are valid.
2. Convert both dates to days (for example days since 1970-01-01).
3. printf the difference
Line 11: You're overlaying the values that were read in at line 8.

Line 2: You have a prototype for dateSerial(), but never implement it. If you want to find the number of days between two dates, you want to take the different between two serial dates. Keep in mind the dates may not be in order.

if both the dates are valid

I see no checking that the dates are valid.

Line 15: You probably want to printf the difference between two dates, rather than returning a calculation to the operating system.





Topic archived. No new replies allowed.