P
priyasmita_guha
Here is a program-
/* PROGRAM: To find the difference between two dates */
#include<dos.h>
#include<stdio.h>
#include<conio.h>
#include<process.h>
void valid_date(int,int,int);
int getkey(void);
void differ_dat(struct date *,struct date *);
const int mth[12]={31,28,31,30,31,30,31,31,30,31,30,31};
void main()
{
struct date dt1,dt2;
int n,n1;
clrscr();
gotoxy(3,15);
printf("Enter a date according to the format dd-mm-yyyy: ");
gotoxy(3,17);
printf("Enter a date according to the format dd-mm-yyyy: ");
gotoxy(54,15);
/* If enter key is hit it implies the system date should be taken into
account */
n=getkey();
if(n!=28)
{
scanf("%d%d%d",&dt1.da_day,&dt1.da_mon,&dt1.da_year);
valid_date(dt1.da_day,dt1.da_mon,dt1.da_year);
gotoxy(54,17);
n1=getkey();
if(n1!=28)
{
scanf("%d%d%d",&dt2.da_day,&dt2.da_mon,&dt2.da_year);
valid_date(dt2.da_day,dt2.da_mon,dt2.da_year);
differ_dat(&dt1,&dt2);
}
else
{
getdate(&dt2);
differ_dat(&dt1,&dt2);
}
}
else
{
getdate(&dt1);
gotoxy(54,17);
scanf("%d%d%d",&dt2.da_day,&dt2.da_mon,&dt2.da_year);
valid_date(dt2.da_day,dt2.da_mon,dt2.da_year);
differ_dat(&dt1,&dt2);
}
}
void valid_date(int day,int month,int year)
{
if((year<1)||(year>9999))
{
clrscr();
gotoxy(10,15);
printf("An invalid year has been entered ");
getch();
exit(1);
}
else if((month<1)||(month>12))
{
clrscr();
gotoxy(10,15);
printf("An invalid month has been entered ");
getch();
exit(1);
}
else if((day<1)||(day>mth[month--]))
{
clrscr();
gotoxy(10,15);
printf("An invalid day has been entered ");
getch();
exit(1);
}
}
int getkey()
{
union REGS i,o;
while(!kbhit());
i.h.ah=0;
int86(22,&i,&o);
return(o.h.ah);
}
void differ_dat(struct date *d1,struct date *d2)
{
clrscr();
printf("\n
1yr=%d\t2yr=%d\t1mon=%d\t2mon=%d\t1day=%d\t2day=%d",d1->da_year,d2->da_year,d1->da_mon,d2->da_mon,d1->da_day,d2->da_day);
if((d1->da_mon==d2->da_mon)&&(d1->da_day==d2->da_day))
{
if(d1->da_year==d2->da_year)
{
gotoxy(5,15);
printf(" There is no difference between the entered dates as they
are the same. ");
getch();
exit(1);
}
else if(d1->da_year>d2->da_year)
{
gotoxy(5,15);
printf("The difference between the two dates is %d years.
",d1->da_year-d2->da_year);
getch();
exit(1);
}
else
{
gotoxy(5,15);
printf("The difference between the two dates is %d years.
",d2->da_year-d1->da_year);
getch();
exit(1);
}
}
if((d1->da_day==d2->da_day)&&(d1->da_year==d2->da_year))
{
if(d1->da_mon>d2->da_mon)
{
gotoxy(5,15);
printf("The difference between the two dates is %d months.
",d1->da_mon-d2->da_mon);
getch();
exit(1);
}
else
{
gotoxy(5,15);
printf("The difference between the two dates is %d months.
",d2->da_mon-d1->da_mon);
getch();
exit(1);
}
}
}
Cannot explain the following-The above program gives me the following
problem-
1)When the first date is asked of the user and the second date is the
system date taken into account the program works fine.
2)When both the dates are asked of the user, the year field of the
first date is automatically converted to 1792,irrespective of the input
in the fn differ_dat.
3)When first date is the system date and the second is an input from
the user,the year field of the system date is automatically converted
to 1792.
Can anyone please explain this uncanny attitude of the program ?
Sorry in advance for having to go through this big problem.
/* PROGRAM: To find the difference between two dates */
#include<dos.h>
#include<stdio.h>
#include<conio.h>
#include<process.h>
void valid_date(int,int,int);
int getkey(void);
void differ_dat(struct date *,struct date *);
const int mth[12]={31,28,31,30,31,30,31,31,30,31,30,31};
void main()
{
struct date dt1,dt2;
int n,n1;
clrscr();
gotoxy(3,15);
printf("Enter a date according to the format dd-mm-yyyy: ");
gotoxy(3,17);
printf("Enter a date according to the format dd-mm-yyyy: ");
gotoxy(54,15);
/* If enter key is hit it implies the system date should be taken into
account */
n=getkey();
if(n!=28)
{
scanf("%d%d%d",&dt1.da_day,&dt1.da_mon,&dt1.da_year);
valid_date(dt1.da_day,dt1.da_mon,dt1.da_year);
gotoxy(54,17);
n1=getkey();
if(n1!=28)
{
scanf("%d%d%d",&dt2.da_day,&dt2.da_mon,&dt2.da_year);
valid_date(dt2.da_day,dt2.da_mon,dt2.da_year);
differ_dat(&dt1,&dt2);
}
else
{
getdate(&dt2);
differ_dat(&dt1,&dt2);
}
}
else
{
getdate(&dt1);
gotoxy(54,17);
scanf("%d%d%d",&dt2.da_day,&dt2.da_mon,&dt2.da_year);
valid_date(dt2.da_day,dt2.da_mon,dt2.da_year);
differ_dat(&dt1,&dt2);
}
}
void valid_date(int day,int month,int year)
{
if((year<1)||(year>9999))
{
clrscr();
gotoxy(10,15);
printf("An invalid year has been entered ");
getch();
exit(1);
}
else if((month<1)||(month>12))
{
clrscr();
gotoxy(10,15);
printf("An invalid month has been entered ");
getch();
exit(1);
}
else if((day<1)||(day>mth[month--]))
{
clrscr();
gotoxy(10,15);
printf("An invalid day has been entered ");
getch();
exit(1);
}
}
int getkey()
{
union REGS i,o;
while(!kbhit());
i.h.ah=0;
int86(22,&i,&o);
return(o.h.ah);
}
void differ_dat(struct date *d1,struct date *d2)
{
clrscr();
printf("\n
1yr=%d\t2yr=%d\t1mon=%d\t2mon=%d\t1day=%d\t2day=%d",d1->da_year,d2->da_year,d1->da_mon,d2->da_mon,d1->da_day,d2->da_day);
if((d1->da_mon==d2->da_mon)&&(d1->da_day==d2->da_day))
{
if(d1->da_year==d2->da_year)
{
gotoxy(5,15);
printf(" There is no difference between the entered dates as they
are the same. ");
getch();
exit(1);
}
else if(d1->da_year>d2->da_year)
{
gotoxy(5,15);
printf("The difference between the two dates is %d years.
",d1->da_year-d2->da_year);
getch();
exit(1);
}
else
{
gotoxy(5,15);
printf("The difference between the two dates is %d years.
",d2->da_year-d1->da_year);
getch();
exit(1);
}
}
if((d1->da_day==d2->da_day)&&(d1->da_year==d2->da_year))
{
if(d1->da_mon>d2->da_mon)
{
gotoxy(5,15);
printf("The difference between the two dates is %d months.
",d1->da_mon-d2->da_mon);
getch();
exit(1);
}
else
{
gotoxy(5,15);
printf("The difference between the two dates is %d months.
",d2->da_mon-d1->da_mon);
getch();
exit(1);
}
}
}
Cannot explain the following-The above program gives me the following
problem-
1)When the first date is asked of the user and the second date is the
system date taken into account the program works fine.
2)When both the dates are asked of the user, the year field of the
first date is automatically converted to 1792,irrespective of the input
in the fn differ_dat.
3)When first date is the system date and the second is an input from
the user,the year field of the system date is automatically converted
to 1792.
Can anyone please explain this uncanny attitude of the program ?
Sorry in advance for having to go through this big problem.