Results 1 to 10 of 10

Thread: can some1 help me?

  1. #1
    Member
    Join Date
    Nov 2001
    Posts
    37

    Question can some1 help me?

    The following program got some bugs. esp, when u key in 31/??/???? . of course ? means any integer from 0 to 9. I hope that u guys can fix the bugs for me. Thanks.

    #include <conio.h>
    #include <stdlib.h>

    void err_mesg()
    {
    _setcursortype(_NOCURSOR);
    gotoxy(2,24); textcolor(14 + BLINK); cprintf("\aInvalid Date Entry");
    if ( getch() == 27) exit(0);
    textattr(0x07); clrscr();
    _setcursortype(_NORMALCURSOR);
    }

    void main()
    {
    clrscr();
    int iday,imonth,iyear,lyear;
    char day[3],month[3],year[5];
    char c,day1,day2,month1,month2,year1,year2,year3,year4;

    top:
    cprintf("\nTo Quit The Program Press Esc");
    cprintf("\n\rEnter the date(DD/MM/YYYY):");

    // Day \\
    cscanf("%c",&day1); cscanf("%c",&day2);
    if (day1 < 48 || day1 > 57) {
    err_mesg();
    goto top;
    }
    day[0] = day1; day[1] = day2; day[2] = '\0';
    iday = atoi(day);
    if (iday < 1 || iday > 31 ) {
    err_mesg();
    goto top;
    }
    // End of Day \\
    cscanf("%c",&c);
    // Month
    cscanf("%c",&month1); cscanf("%c",&month2);
    month[0] = month1; month[1] = month2; month[2] = '\0';
    imonth = atoi(month);
    if (imonth < 1 || imonth > 12 ) {
    err_mesg();
    goto top;
    }
    if (imonth == 2)
    if (iday > 29) {
    err_mesg();
    goto top;
    }
    if (iday > 30) /* here is the bugs */
    if (imonth!=1 || imonth!=3 || imonth!=5 || imonth!=7 || imonth!=8 || imonth!=10 || imonth!=12) {
    err_mesg();
    goto top;
    }
    // End of Month \\
    cscanf("%c",&c);
    // Year \\
    cscanf("%c",&year1); cscanf("%c",&year2); cscanf("%c",&year3); cscanf("%c",&year4);
    year[0] = year1; year[1] = year2; year[2] = year3; year[3] = year4;
    iyear = atoi(year);
    if (iyear < 100 || iyear > 2099) {
    err_mesg();
    goto top;
    }
    if (imonth == 2)
    if (iday == 29) {
    lyear = iyear % 4;
    if (lyear) {
    err_mesg();
    goto top;
    }
    }
    // End of Year \\
    _setcursortype(_NOCURSOR);
    gotoxy(2,24); textcolor(1 + BLINK); cprintf("Thanks For Testing");
    getch(); _setcursortype(_NORMALCURSOR);
    }

  2. #2
    Hi mom!
    Join Date
    Aug 2001
    Posts
    1,103
    What bugs occur? Any error messages?

  3. #3
    Senior Member
    Join Date
    Sep 2001
    Posts
    193
    if (iday > 30) /* here is the bugs */

    Off hand i see no problems.
    What compiler are you using?
    After you compile and run it what error messages does your compiler show you?

    I am also going to compile this program and try and debug it. I will post answer if I find a solution.
    [shadow]l3aDmOnKeY[/shadow]

  4. #4
    Member
    Join Date
    Nov 2001
    Posts
    37

    any idea??

    i also got no error mesg.
    but when u entered 31/03/2002
    this program will prompt "invalid date entry", from the function err_mesg();
    the main problem is u can't enter the day which is greater than 30th.

    So, any idea??

  5. #5
    NYP,
    if (iday < 1 || iday > 31 ) { should be if (iday < 1 || iday > 32 ) { surely

    Or you could change it to less than or equal to 31 take your pick

  6. #6
    Member
    Join Date
    Nov 2001
    Posts
    37
    paulcottingham


    Still doesn't work. I don't think there is a problem with day, coz when I use printf to print out the value of iday and imonth it show the correct value of what I have key in. if u r using Borland Turbo C++ u can juz opened the program and press F7 to trace it step by step.

    Anyway, thanks.

  7. #7
    Hi mom!
    Join Date
    Aug 2001
    Posts
    1,103
    NYP, your error is here:
    Code:
    if (imonth!=1 || imonth!=3 || imonth!=5 || imonth!=7 || imonth!=8 || imonth!=10 || imonth!=12) {
    err_mesg();
    This is always true! (so, err_mesg() will always be called if (iday>30)).

    for example, if imonth equals 7, evaluating that line would result in

    Code:
    if (true || true || true || false || true || true || true)
    this evaluates to true.

  8. #8
    Member
    Join Date
    Nov 2001
    Posts
    37
    Guus

    I think u r right. But how shall I chagne the coding to avoid this kinda problem happened again? Any suggestion to debug it?

  9. #9
    Antionline Herpetologist
    Join Date
    Aug 2001
    Posts
    1,165
    Simple. Just change:

    if (imonth!=1 || imonth!=3 || imonth!=5 || imonth!=7 || imonth!=8 || imonth!=10 || imonth!=12) {
    err_mesg();
    }

    to

    if (imonth==1 || imonth==3 || imonth==5 || imonth==7 || imonth==8 || imonth==10 || imonth==12) {
    ;
    }
    else{
    err_mesg();
    }

    Or
    if (imonth==2 || imonth==4 || imonth==6 || imonth==9 || imonth==11) {
    err_mesg();
    }

  10. #10
    Member
    Join Date
    Nov 2001
    Posts
    37

    Thumbs up

    Thanks everyone. Finally, i found out what's wrong with my program. it is working properly now. Thanks a lot.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •