By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,172 Members | 1,129 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,172 IT Pros & Developers. It's quick & easy.

Cannot explain why the date is changed to 1792.

P: n/a
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_yea r);
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_yea r);
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_yea r);
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.

Feb 16 '06 #1
Share this Question
Share on Google+
13 Replies


P: n/a


pr*************@yahoo.co.in wrote On 02/16/06 16:03,:
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.)

--
Er*********@sun.com

Feb 16 '06 #2

P: n/a
On 16 Feb 2006 13:03:37 -0800, in comp.lang.c ,
"pr*************@yahoo.co.in" <pr*************@yahoo.co.in> wrote:
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_yea r);


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
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Feb 16 '06 #3

P: n/a
"pr*************@yahoo.co.in" wrote:

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
Feb 17 '06 #4

P: n/a
pr*************@yahoo.co.in wrote:
<snip>
void main()
{
struct date dt1,dt2; <snip> scanf("%d%d%d",&dt1.da_day,&dt1.da_mon,&dt1.da_yea r); <snip> scanf("%d%d%d",&dt2.da_day,&dt2.da_mon,&dt2.da_yea r);
} <snip> 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.
Feb 17 '06 #5

P: n/a
Mark McIntyre wrote:
On 16 Feb 2006 13:03:37 -0800, in comp.lang.c ,
"pr*************@yahoo.co.in" <pr*************@yahoo.co.in> wrote:

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_yea r);

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.

--
st

Feb 17 '06 #6

P: n/a
specter tator <st****@spamcop.com> writes:
[snip]
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 17 '06 #7

P: n/a
specter tator wrote:
Mark McIntyre wrote:
"pr*************@yahoo.co.in" <pr*************@yahoo.co.in> wrote:
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_yea r);


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


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?

--
"The most amazing achievement of the computer software industry
is its continuing cancellation of the steady and staggering
gains made by the computer hardware industry..." - Petroski

Feb 17 '06 #8

P: n/a
On Fri, 17 Feb 2006 04:41:49 -0500, in comp.lang.c , CBFalconer
<cb********@yahoo.com> wrote:
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
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Feb 17 '06 #9

P: n/a
On Thu, 16 Feb 2006 21:01:11 -0500, in comp.lang.c , CBFalconer
<cb********@yahoo.com> wrote:
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
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Feb 17 '06 #10

P: n/a
Mark McIntyre wrote:
On Fri, 17 Feb 2006 04:41:49 -0500, in comp.lang.c , CBFalconer
<cb********@yahoo.com> wrote:

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


Indeed.
Feb 18 '06 #11

P: n/a
Mark McIntyre <ma**********@spamcop.net> writes:
On Fri, 17 Feb 2006 04:41:49 -0500, in comp.lang.c , CBFalconer
<cb********@yahoo.com> wrote:
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


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

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 18 '06 #12

P: n/a
Thanks for the trouble.The quotation was really enlightening.

Feb 18 '06 #13

P: n/a
"pr*************@yahoo.co.in" <pr*************@yahoo.co.in> writes:
Thanks for the trouble.The quotation was really enlightening.


The quotation was Mark's sig quote:

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan

priyasmita, please read <http://cfaj.freeshell.org/google/>.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 18 '06 #14

This discussion thread is closed

Replies have been disabled for this discussion.