In last month’s article entitled “Single Element Antenna Program (for TS1000)” it was stated that this month would offer some elementary improvements to that program. Here are some suggestions for improving the error handling capabilities of that program.
It is always a good practice to make your programs reject all input of a type which is not expected. In fact in the reviews of software in most magazines one section is often devoted to the error handling capabilities of the program. Anybody who has watched another person enter data knows why error handling is such an important topic.
The program featured last month has three places where it expects input. The first is in line 12. That, and the surrounding few lines are reprinted here for reference.
6 PRINT "SELECT FOLLOWING"
7 PRINT "1/4 W PRESS 1"
8 PRINT "1/2 W PRESS 2"
9 PRINT '5/8 W PRESS 3"
10 PRINT "3/4 W PRESS 4"
12 IF INKEY$ = "" THEN GOTO 12
13 LET A$ = INKEY$
15 IF A$ = "1" THEN LET K = 234000
18 IF A$ = "2" THEN LET K = 468000
20 IF A$ = "3" THEN LET K = 585000
25 IF A$ = "4" THEN LET K = 702000
30 PAUSE 50
35 CLS
In line 12, one is asked for one of four numbers designated in the provious lines. This number is used to set a particular value of K. But what happens if the input is not one of the four previously defined numbers? This inexpected value (now put in the variable A$ by line 13) falls through all the four IF statements (lines # 15, 18, 20, and 25) and continues from line 30. Thus the program runs to its conclusion with the value of K still containing 0, thus producing nonsense.
What one needs to do is return the program to line 12 after a warning message if the input is unacceptable. It is probably best to return the program to line 6 so that the allowed choices are again presented. This procedure will add many lines to the length of the progrqam but will make it more user friendly. Try the following lines.
15 IF A$ = "1" THEN GOTO 28
18 IF A$ = "2" THEN GOTO 30
20 IF A$ = "3" THEN GOTO 32
22 IF A$ = "4" THEN GOTO 34
23 PRINT "YOU DID NOT ENTER ONE
OF THE REQUIRED NUMBERS"
24 PRINT "PUSH ANY KEY TO TRY AGAIN"
25 IF INKEY$ = "" THEN GOTO 25
26 CLS
27 GOTO 6
28 LET K = 234000
29 GOTO 35
30 LET K = 468000
31 GOTO 35
32 LET K = 585000
33 GOTO 35
34 LET K = 702000
35 CLS
Here we have added 10 lines; but, if the input is an unexpected character, the program starts the imput sequence over. Note that the program gives the user some time to realize that something has gone wrong, continues only when the user wishes, and goes back far enough for the user to see the original instructions.
The next input line is number 125. That, and the surrounding few lines are reprinted here for reference.
120 PRINT "ARE ALL ENTRIES OK? Y/N"
125 IF INKEY$ = "" THEN GOTO 125
130 LET C$ = INKEY$
132 PAUSE 20
135 IF C$ = "Y" THEN GOTO 143
140 IF C$ = "N" THEN GOTO 35
143 CLS
Here only a simple yes or no (Y/N) is expected. As the program was first written, one got the same response for any key except N. That is, any key except N is the equivalent of Y. This state of affairs can be corrected by the addition of two lines.
141 PRINT "YOU MUST ANSWER WITH
EITHER A Y OR AN N"
142 GOTO 120
Note that line 141 implies the difficulty with the first response and tells the user how to correct it. Also one has to carefully consider the lines which appear on the screen so that messages are not flashed on and off too rapidly and so that all the information you wish is on the screen at the time you wish it to be.
The third input line is number 355. That line and the surrounding few lines are reproduced here for reference.
345 PRINT "PRESS 1 FOR NEW DESIGN"
350 PRINT "PRESS 2 TO STOP"
355 IF INKEY$ = "" THEN GOTO 355
360 LET B$ = INKEY$
365 IF B$ = "1" THEN GOTO 375
370 IF B$ = "2" THEN GOTO 390
375 CLS
380 GOTO 6
386 PRINT "MAX CALCULATION
LIMIT REACHED"
387 GOTO 340
390 STOP
Here one also has only two choices, but, in this case, any number except 2 will start the program over. Here the addition of three lines will correct the problem. Try
372 CLS
373 PRINT "PLEASE SELECT EITHER 1 OR 2"
374 GOTO 345
Again note that one has to pay a bit of attention to what the screen looks like if the program is to operate smoothly.
The basic idea behind all these input error checking hints is to consider the flow of the program when unexpected information is input. These ideas will work in many other circumstances than that of the examples used here. Try you hand at applying them to the program on transmission line lengths in this issue.