Cannot explain why the date is changed to 1792.

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.
 
E

Eric Sosman

Here is a program-

Maybe so. It's not a C program, though: it's some
kind of "C with a lot of non-standard system-specific
stuff thrown in." Try taking your problem to a newsgroup
that deals with system-specific extensions, rather than
with Standard C.

(In particular, the code appears to be doing some
sort of direct I/O through non-C facilities, which may
or may not interfere with printf(), scanf(), and the
rest of C's I/O machinery.)
 
M

Mark McIntyre

Here is a program-

Unfortunately your code is littered with unnecessary function calls to
DOS specific stuff (such as gotoxy, clrscr etc) which makes it very
very hard to diagnose. You should get it working before worrying about
formatting output, and making the screen look pretty.

So... reduce it to the smallest simple commandline programme that
exhibits the problem, and then repost it.
void main()

main must return an int.
{
struct date dt1,dt2;

There's no such struct in C thought if you supply the definition, your
code would be ok. Alternatively, why not use struct tm?
scanf("%d%d%d",&dt1.da_day,&dt1.da_mon,&dt1.da_year);

First, do not use scanf for user input. Please read the FAQ for some
ideas why not, and you have actually discovered it yourself.
For instance this won't read a string dd-mm-yyyy because for example
the "-12" of "12-12-2005" is a valid integer for %d.....

Recommended practice is to use fgets to read in a string, then parse
it carefully. reject bad data, and even read the \n directly from the
string instead of having to use DOS registers etc.

The rest of your problem is probably now solved...
Mark McIntyre
 
C

CBFalconer

Here is a program-
/* PROGRAM: To find the difference between two dates */
#include<dos.h>
#include<stdio.h>
#include<conio.h>
#include<process.h>

Since dos.h, conio.h, and process.h are non-standard and you did
not give their contents (in purely standard c) we have no idea what
is going on. You need to find a newsgroup that deals with your
system. Alternatively you could try rewriting your program to use
the portable facilities of standard C. Start by investigating what
is defined in time.h and/or reading your C book.

--
"The power of the Executive to cast a man into prison without
formulating any charge known to the law, and particularly to
deny him the judgement of his peers, is in the highest degree
odious and is the foundation of all totalitarian government
whether Nazi or Communist." -- W. Churchill, Nov 21, 1943
 
P

Peter Slootweg

void main()
{
struct date dt1,dt2;
scanf("%d%d%d",&dt1.da_day,&dt1.da_mon,&dt1.da_year);
scanf("%d%d%d",&dt2.da_day,&dt2.da_mon,&dt2.da_year);
}
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.

assuming

struct date {
short da_year;
char da_day;
char da_mon;
};

your format string to scanf indicates you will pass in the addresses for
3 ints. you are actually passing in addresses for 2 chars and 1 short.
If you have a C99 compiler you can change the format specifiers to
reflect the actual types - or if you don't have a C99 compiler then you
will probably need to declare 3 int variables.
 
S

specter tator

Mark said:
Unfortunately your code is littered with unnecessary function calls to
DOS specific stuff (such as gotoxy, clrscr etc) which makes it very
very hard to diagnose. You should get it working before worrying about
formatting output, and making the screen look pretty.

So... reduce it to the smallest simple commandline programme that
exhibits the problem, and then repost it.




main must return an int.




There's no such struct in C thought if you supply the definition, your
code would be ok. Alternatively, why not use struct tm?




First, do not use scanf for user input. Please read the FAQ for some
ideas why not, and you have actually discovered it yourself.
For instance this won't read a string dd-mm-yyyy because for example
the "-12" of "12-12-2005" is a valid integer for %d.....

Recommended practice is to use fgets to read in a string, then parse
it carefully. reject bad data, and even read the \n directly from the
string instead of having to use DOS registers etc.

The rest of your problem is probably now solved...
Mark McIntyre

Instead of harshly replying with something like this ...

"Since dos.h, conio.h, and process.h are non-standard and you did
not give their contents (in purely standard c) we have no idea what
is going on. You need to find a newsgroup that deals with your
system. Alternatively you could try rewriting your program to use
the portable facilities of standard C. Start by investigating what
is defined in time.h and/or reading your C book."

.... Mr. McIntyre responds in a way that accomplishes both polite
admonition and helpful advice to the OP.

I wish there were more people like you in this world in general and in
this newsgroup in specific, Mr. McIntyre.
 
K

Keith Thompson

specter tator said:
Instead of harshly replying with something like this ...

"Since dos.h, conio.h, and process.h are non-standard and you did
not give their contents (in purely standard c) we have no idea what
is going on. You need to find a newsgroup that deals with your
system. Alternatively you could try rewriting your program to use
the portable facilities of standard C. Start by investigating what
is defined in time.h and/or reading your C book."

... Mr. McIntyre responds in a way that accomplishes both polite
admonition and helpful advice to the OP.

On the other hand, the reply you quoted was also perfectly
appropriate.
 
C

CBFalconer

specter said:
Instead of harshly replying with something like this ...

"Since dos.h, conio.h, and process.h are non-standard and you did
not give their contents (in purely standard c) we have no idea what
is going on. You need to find a newsgroup that deals with your
system. Alternatively you could try rewriting your program to use
the portable facilities of standard C. Start by investigating what
is defined in time.h and/or reading your C book."

... Mr. McIntyre responds in a way that accomplishes both polite
admonition and helpful advice to the OP.

I wish there were more people like you in this world in general and in
this newsgroup in specific, Mr. McIntyre.

And why do you equate succint with harsh? Was there no advice
given? Was that advice inaccurate? Was the reasoning obscure?
Was the reply rude or demeaning?
 
M

Mark McIntyre

And why do you equate succint with harsh? Was there no advice
given? Was that advice inaccurate? Was the reasoning obscure?
Was the reply rude or demeaning?

I think you and Keith may have misinterpreted the previous poster's
post. I have a feeling he was complimenting me on being helpful yet
redirective and even polite. :)
Mark McIntyre
 
M

Mark McIntyre

Since dos.h, conio.h, and process.h are non-standard and you did
not give their contents (in purely standard c) we have no idea what
is going on. You need to find a newsgroup that deals with your
system. Alternatively you could try rewriting your program to use
the portable facilities of standard C. Start by investigating what
is defined in time.h and/or reading your C book.

Just for the record, I DO NOT consider CBF's response harsh,
inaccurate, obscure, rude or demeaning. I'm happy that my own post was
complimented but when I replied to CBF's earlier post I had not
realised that "spector ator" was being rude about CBF's. Had I done
so, I would have responded differently.
Mark McIntyre
 
S

specter tator

Mark said:
I think you and Keith may have misinterpreted the previous poster's
post. I have a feeling he was complimenting me on being helpful yet
redirective and even polite. :)
Mark McIntyre

Indeed.
 
K

Keith Thompson

Mark McIntyre said:
I think you and Keith may have misinterpreted the previous poster's
post. I have a feeling he was complimenting me on being helpful yet
redirective and even polite. :)
Mark McIntyre

I interpreted the previous post to mean that Mark's response was
appropriate (with which I agree), and implicitly that CBFalconer's
response was not (with which I disagree).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top