-
January 8th, 2002, 04:33 PM
#1
Member
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);
}
-
January 8th, 2002, 04:41 PM
#2
What bugs occur? Any error messages?
-
January 10th, 2002, 02:36 AM
#3
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]
-
January 10th, 2002, 05:52 PM
#4
Member
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??
-
January 10th, 2002, 05:57 PM
#5
Member
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
-
January 10th, 2002, 06:39 PM
#6
Member
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.
-
January 11th, 2002, 02:21 PM
#7
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.
-
January 13th, 2002, 02:22 PM
#8
Member
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?
-
January 13th, 2002, 03:43 PM
#9
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();
}
-
January 14th, 2002, 04:48 PM
#10
Member
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
-
Forum Rules
|
|