Program to compute difference between two dates

#include "stdio.h"
#include "math.h"
void main()
{
int day1,mon1,year1,day2,mon2,year2;
int ref,dd1,dd2,i;
clrscr();
printf("Enter first day, month, year");
scanf("%d%d%d",&day1,&mon1,&year1);
scanf("%d%d%d",&day2,&mon2,&year2);
ref = year1;
if(year2ref = year2;
dd1=0;
dd1=func1(mon1);
for(i=ref;i{
if(i%4==0)
dd1+=1;
}
dd1=dd1+day1+(year1-ref)*365;
printf("No. of days of first date fronm the Jan 1 %d= %d",year1,dd1);
/* Count for additional days due to leap years*/
dd2=0;
for(i=ref;i{
if(i%4==0)
dd2+=1;
}
dd2=func1(mon2)+dd2+day2+((year2-ref)*365);
printf("No. of days from the reference year's first Jan = %d",dd2);
printf("Therefore, diff between the two dates is %d",abs(dd2-dd1));

getch();
}




int func1(x) //x for month y for dd
{ int y=0;
switch(x)
{
case 1: y=0; break;
case 2: y=31; break;
case 3: y=59; break;
case 4: y=90; break;
case 5: y=120;break;
case 6: y=151; break;
case 7: y=181; break;
case 8: y=212; break;
case 9: y=243; break;
case 10:y=273; break;
case 11:y=304; break;
case 12:y=334; break;
default: printf("Error encountered"); exit(1);
}
return(y);
}

7 comments:

  1. sorry this doesnt work...

    ReplyDelete
  2. This looks absolutely horrible!

    ReplyDelete
  3. Nope not working, I think it just needs some rework. Tried to compile on Dev-C (windows) and in native gcc (Linux) both failed, mostly getting parameter list declarations errors, along with conflicting types, and return types not being correct. Here is the output from GCC directly:

    computeyears.c: In function `main':
    computeyears.c:13: error: syntax error before "ref"
    computeyears.c:17: error: syntax error before '{' token
    computeyears.c:4: warning: return type of 'main' is not `int'
    computeyears.c: At top level:
    computeyears.c:21: error: `day1' undeclared here (not in a function)
    computeyears.c:21: error: `year1' undeclared here (not in a function)
    computeyears.c:21: error: `ref' undeclared here (not in a function)
    computeyears.c:21: warning: data definition has no type or storage class
    computeyears.c:22: error: syntax error before string constant
    computeyears.c:22: error: conflicting types for 'printf'
    computeyears.c:22: note: a parameter list with an ellipsis can't match an empty parameter name list declaration
    computeyears.c:22: error: conflicting types for 'printf'
    computeyears.c:22: note: a parameter list with an ellipsis can't match an empty parameter name list declaration
    computeyears.c:22: warning: data definition has no type or storage class
    computeyears.c:24: warning: data definition has no type or storage class
    computeyears.c:25: error: syntax error before "for"
    computeyears.c:26: error: syntax error before '{' token
    computeyears.c:30: error: redefinition of 'dd2'
    computeyears.c:24: error: previous definition of 'dd2' was here
    computeyears.c:30: error: `mon2' undeclared here (not in a function)
    computeyears.c:30: error: `day2' undeclared here (not in a function)
    computeyears.c:30: error: `year2' undeclared here (not in a function)
    computeyears.c:30: warning: data definition has no type or storage class
    computeyears.c:31: error: syntax error before string constant
    computeyears.c:31: warning: data definition has no type or storage class
    computeyears.c:32: error: syntax error before string constant
    computeyears.c:34: warning: data definition has no type or storage class
    computeyears.c:35: error: syntax error before '}' token
    computeyears.c:59:3: warning: no newline at end of file

    ReplyDelete
  4. Friend actually when i post this programs some part in TAG is omitted by blogger, i dont know the reason, but it happning,so i have posted all this program in my new site : www.glearn.net
    please try the program from there...sorry for problem.. thanks

    ReplyDelete
  5. all of u r idiot
    just make some changes
    this program will work currectly
    n there r not that much errors asshown above
    only 3-4 errors were there

    ReplyDelete
  6. can I ask what would be the output of this program. this is for our project. please help me.

    ReplyDelete