473,414 Members | 1,643 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Cannot explain why the date is changed to 1792.

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
13 2173


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
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
"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
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
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
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
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
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
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
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
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
Thanks for the trouble.The quotation was really enlightening.

Feb 18 '06 #13
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Sandy | last post by:
I am not sure how this even changed but... Until just recently when I went into any database and listed stored procedures I could list by the date which was in the format: mm/dd/yyyy. This...
3
by: VivN | last post by:
I have an unbound form (Access 2000 ADP front end/SQL server backend) to collect parameters, one of which is a date. The date is being reordered to mm/dd/yyyy if it involves any day up to 12th of...
6
by: BStorm | last post by:
Is it possible to change individual RowState properties from updated to not updated?
5
by: Bill Cohagan | last post by:
I'm constructing an ASP app with a frameset on the home html page. From frame A I need to referesh the page in frame B when a button click occurs on a button in Frame A (server side event handler)....
5
by: Jurgen Oerlemans | last post by:
Hello, I created an array of files in a directory by doing a: Dim FileArray As String() = IO.Directory.GetFiles(QueueSource, "*.Q") My question is: How can I sort this array on the...
3
by: Yuri | last post by:
Does anybody have a tool to store/retrieve explained sql? We're running DB2 8.2 on AIX. We would like to be able to compare old saved explains to the current ones and see if anything changed....
0
by: JAW | last post by:
This plan seems like it should perform well. Does anyone see anything. SQL Statement Text: DECLARE MTR - RDG - EST - CSR CURSOR FOR
1
by: rdsandy | last post by:
Hi, I have a table Risk, a table Mitigation and a table RiskArchive. I am taking the RiskID, Criticality and MitigationPlan fields from Risk, and MitigationActionID from Mitigation and inserting...
5
by: Just_a_fan | last post by:
I tried to put an "on error" statement in a routine and got the message that I cannot user "on error" and a lamda or query expression in the same routine. Help does not list anything useful for...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.