Here is a program-
/* PROGRAM: To find the difference between two dates */
#include<dos.h>
#include<stdio. h>
#include<conio. h>
#include<proces s.h>
void valid_date(int, int,int);
int getkey(void);
void differ_dat(stru ct date *,struct date *);
const int mth[12]={31,28,31,30,3 1,30,31,31,30,3 1,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,&dt 1.da_mon,&dt1.d a_year);
valid_date(dt1. da_day,dt1.da_m on,dt1.da_year) ;
gotoxy(54,17);
n1=getkey();
if(n1!=28)
{
scanf("%d%d%d", &dt2.da_day,&dt 2.da_mon,&dt2.d a_year);
valid_date(dt2. da_day,dt2.da_m on,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,&dt 2.da_mon,&dt2.d a_year);
valid_date(dt2. da_day,dt2.da_m on,dt2.da_year) ;
differ_dat(&dt1 ,&dt2);
}
}
void valid_date(int day,int month,int year)
{
if((year<1)||(y ear>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)||(da y>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(stru ct date *d1,struct date *d2)
{
clrscr();
printf("\n
1yr=%d\t2yr=%d\ t1mon=%d\t2mon= %d\t1day=%d\t2d ay=%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,irrespecti ve 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. 13 2188 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
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,&dt 1.da_mon,&dt1.d a_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
--
"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 =----
"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<proces s.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 pr************* @yahoo.co.in wrote:
<snip> void main() { struct date dt1,dt2;
<snip> scanf("%d%d%d", &dt1.da_day,&dt 1.da_mon,&dt1.d a_year);
<snip> scanf("%d%d%d", &dt2.da_day,&dt 2.da_mon,&dt2.d a_year); }
<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,irrespecti ve 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.
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,&dt 1.da_mon,&dt1.d a_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
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
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_Keit h) 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.
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,&dt 1.da_mon,&dt1.d a_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...
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
On Fri, 17 Feb 2006 04:41:49 -0500, in comp.lang.c , CBFalconer
<cb********@yah oo.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 =----
On Thu, 16 Feb 2006 21:01:11 -0500, in comp.lang.c , CBFalconer
<cb********@yah oo.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 =---- This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 allowed me to find the most recent stored procedures as they could be
sorted to the top.
Somehow the date has been changed to: yyyy-mm-dd hh:mm:ss:kkk...
|
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 the month. Thus the query fails as it is passing the wrong
date.
My regional setting are definately UK english and my short date format
is...
|
by: BStorm |
last post by:
Is it possible to change individual RowState properties from updated to not
updated?
|
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).
To accomplish this I've included some client side script in the page being
built in frame A such that whenever it is received by the browser it...
|
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 create-date of the files?
Best regards, Jurgen Oerlemans
| |
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.
Thanks,
Yuri
|
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
|
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 them into RiskArchive, using a stored procedure. What happens is if there is no entry in RiskArchive for a specific RiskID, and the other fields...
|
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 explaining a "lamda" expression
and so I don't know what one is and I am not doing any database stuff in
the entire program.
So what does this...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |