Practice Program 6.2 to detect incorrect inputs for dates:

! Practice Programs 2 and 3 in Chapter 5 ask a user to enter a date in MM-DD-YY

! format. The month and day may be specified by single digits---that is,

! 1-5-85 rather than 01-05-85

!

! Write a program that requests such a date and then checks the validity of the

! entry. If a user enters an invalid date, the program should loop back and allow

! the date to be entered again. Assume 29 days in February.

!

! Test your program using the following date values:

! 13-20-85

! 2-30-81

! 11-31-88

! 1-17-77

!

! Pseudocode:

! Include tests for

! 1. month between 1 and 12 to catch 13-20-85

! 2. no leading zeros in month or day to catch 01-04-90

! 3. day 1-30 for (9,4,6,11) to catch 11-31-88

! 4. day 1-31 for (1,3,7,8,10) to catch 1-32-99

! 5. day 1-29 for (2) to catch 2-30-81

! If any test fails ask for new input.

! If the month,day,year pass all tests then print the date.

 

LET noerror$ = "false"

DO until noerror$="true"

PRINT "Please enter a date as MM-DD-YY, eg. 1-30-87."

PRINT

PRINT "Use only dates in the twentieth century."

PRINT

LINE INPUT when$

! test when$ for errors here

! if there are errors ask user to reenter the date

! if there are no errors, continue

! error block: if no errors set noerror$ = "true"

 

IF when$[1:1]="0" then

PRINT "The month should not begin with '0'"

ELSE IF when$[pos(when$,"-")+1:pos(when$,"-")+1]="0" then

PRINT "The day should not begin with '0'"

ELSE IF val(when$[1:pos(when$,"-")-1])<1 then

PRINT "The month should be between 1 and 12"

ELSE IF val(when$[1:pos(when$,"-")-1])>12 then

PRINT "The month should be between 1 and 12"

ELSE IF val(when$[1:pos(when$,"-")-1]) = 9 and val(when$[pos(when$,"-")+1:posr(when$,"-")-1]) > 30 then

PRINT "September has no more than 30 days"

ELSE IF val(when$[1:pos(when$,"-")-1]) = 4 and val(when$[pos(when$,"-")+1:posr(when$,"-")-1]) > 30 then

PRINT "April has no more than 30 days"

ELSE IF val(when$[1:pos(when$,"-")-1]) = 6 and val(when$[pos(when$,"-")+1:posr(when$,"-")-1]) > 30 then

PRINT "June has no more than 30 days"

ELSE IF val(when$[1:pos(when$,"-")-1]) = 11 and val(when$[pos(when$,"-")+1:posr(when$,"-")-1]) > 30 then

PRINT "November has no more than 30 days"

ELSE IF val(when$[pos(when$,"-")+1:posr(when$,"-")-1]) > 31 then

PRINT "No month has no more than 31 days"

ELSE IF val(when$[1:pos(when$,"-")-1]) = 2 and val(when$[pos(when$,"-")+1:posr(when$,"-")-1]) > 29 then

PRINT "February has no more than 29 days"

ELSE

PRINT "The date is in correct format"

LET noerror$ = "true"

END IF

PRINT "noerror$ is ..."; noerror$

LOOP

 

PRINT

PRINT "The date you entered is"; " "; when$

PRINT

CALL month (when$, month$)

CALL day (when$, day$)

CALL year (when$, year$)

PRINT "This date may also be written as ";"month$";" ";"day$";",";" ";"year$"

PRINT

 

PRINT "In your case, the date would be ";month$;" ";day$;",";year$

END

 

SUB month (When$, month$) ! note the variable names are irrelevant

LET month$ = When$[1 : pos(When$,"-") - 1]

IF month$ = "1" then LET month$ = "January"

IF month$ = "2" then LET month$ = "February"

IF month$ = "3" then LET month$ = "March"

IF month$ = "4" then LET month$ = "April"

IF month$ = "5" then LET month$ = "May"

IF month$ = "6" then LET month$ = "June"

IF month$ = "7" then LET month$ = "July"

IF month$ = "8" then LET month$ = "August"

IF month$ = "9" then LET month$ = "September"

IF month$ = "10" then LET month$ = "October"

IF month$ = "11" then LET month$ = "November"

IF month$ = "12" then LET month$ = "December"

END SUB

 

SUB day (When$, Day$)

LET Day$ = When$[pos(When$,"-") + 1 : posr(When$,"-") - 1 ]

END SUB

 

SUB year (When$, Year$)

LET Year$ = When$[posr(When$,"-") + 1 : 100]

LET Year$ = "19" & Year$

END SUB