PDA

Click to See Complete Forum and Search --> : can some1 help me?


NYP
January 8th, 2002, 04:33 PM
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);
}

Guus
January 8th, 2002, 04:41 PM
What bugs occur? Any error messages?

l3aDmOnKeY
January 10th, 2002, 02:36 AM
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.:confused:

NYP
January 10th, 2002, 05:52 PM
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??

paulcottingham
January 10th, 2002, 05:57 PM
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

NYP
January 10th, 2002, 06:39 PM
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.

Guus
January 11th, 2002, 02:21 PM
NYP, your error is here:
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

if (true || true || true || false || true || true || true)

this evaluates to true.

NYP
January 13th, 2002, 02:22 PM
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?

cgkanchi
January 13th, 2002, 03:43 PM
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();
}

NYP
January 14th, 2002, 04:48 PM
Thanks everyone. Finally, i found out what's wrong with my program. it is working properly now. Thanks a lot. :D