Cannot explain why the date is changed to 1792.

Discussion in 'C Programming' started by priyasmita_guha@yahoo.co.in, Feb 16, 2006.

  1. Guest

    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.
     
    , Feb 16, 2006
    #1
    1. Advertising

  2. Eric Sosman Guest

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

    --
     
    Eric Sosman, Feb 16, 2006
    #2
    1. Advertising

  3. On 16 Feb 2006 13:03:37 -0800, in comp.lang.c ,
    "" <> 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_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 =----
     
    Mark McIntyre, Feb 16, 2006
    #3
  4. CBFalconer Guest

    "" 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
     
    CBFalconer, Feb 17, 2006
    #4
  5. wrote:
    <snip>
    > void main()
    > {
    > struct date dt1,dt2;

    <snip>
    > scanf("%d%d%d",&dt1.da_day,&dt1.da_mon,&dt1.da_year);

    <snip>
    > scanf("%d%d%d",&dt2.da_day,&dt2.da_mon,&dt2.da_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,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.
     
    Peter Slootweg, Feb 17, 2006
    #5
  6. Mark McIntyre wrote:

    > On 16 Feb 2006 13:03:37 -0800, in comp.lang.c ,
    > "" <> 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_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, Feb 17, 2006
    #6
  7. specter tator <> 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) <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.
     
    Keith Thompson, Feb 17, 2006
    #7
  8. CBFalconer Guest

    specter tator wrote:
    > Mark McIntyre wrote:
    >> "" <> 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_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
     
    CBFalconer, Feb 17, 2006
    #8
  9. On Fri, 17 Feb 2006 04:41:49 -0500, in comp.lang.c , CBFalconer
    <> 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 =----
     
    Mark McIntyre, Feb 17, 2006
    #9
  10. On Thu, 16 Feb 2006 21:01:11 -0500, in comp.lang.c , CBFalconer
    <> 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 =----
     
    Mark McIntyre, Feb 17, 2006
    #10
  11. Mark McIntyre wrote:
    > On Fri, 17 Feb 2006 04:41:49 -0500, in comp.lang.c , CBFalconer
    > <> 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.
     
    specter tator, Feb 18, 2006
    #11
  12. Mark McIntyre <> writes:
    > On Fri, 17 Feb 2006 04:41:49 -0500, in comp.lang.c , CBFalconer
    > <> 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) <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.
     
    Keith Thompson, Feb 18, 2006
    #12
  13. Guest

    Thanks for the trouble.The quotation was really enlightening.
     
    , Feb 18, 2006
    #13
  14. "" <> 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) <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.
     
    Keith Thompson, Feb 18, 2006
    #14
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    713
    Rosanne
    Oct 11, 2005
  2. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,295
    Michael Borgwardt
    May 30, 2004
  3. randpoly.com PostMaster
    Replies:
    0
    Views:
    290
    randpoly.com PostMaster
    Mar 21, 2006
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,072
    Smokey Grindel
    Dec 2, 2006
  5. Replies:
    41
    Views:
    2,282
Loading...

Share This Page