Data Entry and Input Validation in Atari BASIC

Atari text screen showing a form with Name, Age and Join Club fields; BOB is entered as the name and the message 'DIGITS ONLY, PLEASE' appears at the bottom

Users press the wrong keys. They type letters where a number belongs, press RETURN on an empty line, lean on BREAK, or move the cursor up the screen and press RETURN on something else. A program that stops with ERROR- 8 AT LINE 120 has failed them. This guide covers the habits and routines that keep an Atari BASIC program on its feet: understanding what INPUT really does, checking everything before using it, and replacing INPUT with a small field editor when you want proper forms.

What you'll learn: INPUT's behaviour with commas, long strings and empty lines, three ways to avoid ERROR 8, validating numbers and ranges, flushing stray keypresses, and a reusable GET-based field editor with digits-only fields, backspace and error messages.

What you need: Atari BASIC on any Atari 8-bit or emulator. It helps to have read Reading the Keyboard (for GET and location 764) and Error Trapping in Atari BASIC (for TRAP); this guide builds on both.

1. What INPUT actually does

INPUT reads one whole line from the screen editor (up to RETURN) into BASIC's line buffer, then hands the contents out to the variables you listed:

  • Numeric variables are converted one at a time, separated by commas. If the characters don't form a number, or something other than a comma or the end of the line follows the number, INPUT stops with ERROR 8. That includes an empty line: pressing RETURN alone at INPUT N is an error too.
  • String variables take everything up to the end of the line, commas included. INPUT A$ given HELLO, WORLD stores all twelve characters. That differs from READ, where a comma ends a string.
  • Strings longer than the DIM are cut off without any error. With DIM A$(5), typing ABCDEFGH stores ABCDE.
  • The ? prompt appears only for plain INPUT, which reads channel 0. INPUT #2,A$ from a channel you opened to E: shows no prompt, so you can design your own. Opening E: rebuilds the text screen, though, so do it before you draw anything.
10 DIM A$(5),B$(20)
20 TRAP 200:PRINT "NUMBER";:INPUT N:PRINT "N=";N
30 PRINT "TEXT";:INPUT B$:PRINT "B$=[";B$;"]"
40 PRINT "SHORT";:INPUT A$:PRINT "A$=[";A$;"]"
45 TRAP 210:PRINT "EMPTY";:INPUT N:PRINT "N=";N
50 END
200 PRINT "ERROR ";PEEK(195):GOTO 30
210 PRINT "ERROR ";PEEK(195):END

Type ABC, then HELLO, WORLD, then ABCDEFGH, then just RETURN. You will see ERROR 8, the full text with its comma, the truncated ABCDE and ERROR 8 again.

Two other INPUT surprises are worth knowing. Numeric INPUT accepts anything the floating-point package understands, so 1E3, -5 and 3.7 all get through to a variable you expected to hold a whole number. And because INPUT reads from the screen editor, a user who moves the cursor up and presses RETURN sends that other line instead. Finally, CTRL-3 is the keyboard's end-of-file key: pressed at an INPUT it ends the program with ERROR 136.

2. Three ways to avoid ERROR 8

Trap and ask again. The quickest fix is to make the INPUT line its own TRAP target, so a bad entry simply repeats the question:

100 TRAP 100:PRINT "HOW MANY PLAYERS (1-4)";:INPUT P
110 IF P<1 OR P>4 OR P<>INT(P) THEN 100
120 TRAP 40000

Line 110 catches what TRAP can't: out-of-range values and fractions. Line 120 switches trapping off again (any line number above 32767 does that) so a later bug isn't silently swallowed.

Read a string, then check it. INPUT into a string never raises ERROR 8, whatever is typed, so you get the chance to inspect the text yourself and give a specific message:

120 IF LEN(I$)=0 THEN PRINT "EMPTY":RETURN
130 OK=1:FOR I=1 TO LEN(I$):C=ASC(I$(I,I)):IF C<48 OR C>57 THEN OK=0
140 NEXT I:IF NOT OK THEN PRINT "WHOLE NUMBERS ONLY":RETURN
150 Q=VAL(I$):IF Q<1 OR Q>99 THEN PRINT "1 TO 99, PLEASE":RETURN
160 PRINT "OK ";Q:RETURN

Call it after INPUT I$. It accepts only the digits 0–9 (ATASCII 48–57), so 4X, -3 and 1E3 are all refused before VAL sees them. The empty-string test in line 120 must come first. An Atari BASIC FOR loop always runs at least once, so FOR I=1 TO 0 would try to read I$(1,1) from an empty string and stop with ERROR 5. Run against the inputs 42, ABC, (empty), 4X, 100, 0 and 7, this routine accepts only 42 and 7.

Don't use INPUT at all. Read single keys with GET and refuse bad ones as they are typed. That's the field editor in section 4, and it's what commercial programs did.

3. Little things that prevent big problems

Flush the keyboard before important questions. Location 764 holds the last key pressed until something reads it. A player hammering the fire button and the space bar at the end of a game can have a keypress waiting that instantly answers your "PLAY AGAIN (Y/N)?". POKE 764,255 throws it away. Do this just before every yes/no question.

Accept both cases. ATASCII lower-case letters are 97–122. IF K>96 AND K<123 THEN K=K-32 turns them into capitals, so y works as well as Y.

Tell the user what went wrong. "1 TO 99, PLEASE" is better than repeating the question in silence. A short low buzz (SOUND 0,200,12,8 for half a second) draws the eye to the message.

Keep BREAK away from data. The BREAK key stops a program even with TRAP set. In a finished program, clear bit 7 of POKMSK (16) and IRQEN (53774):

2000 B=PEEK(16):IF B>127 THEN B=B-128
2010 POKE 16,B:POKE 53774,B:RETURN

The OS turns BREAK back on with every GRAPHICS command, every OPEN of S: or E: and the first PRINT after it, and SYSTEM RESET. So call line 2000 after drawing your screen, and again after anything that reopens it. Reading the Keyboard covers the details.

4. A field editor for forms

This program lays out a form with POSITION, then fills each field through one subroutine. The subroutine draws dots to show the field's length and shows its own block cursor. It accepts only suitable keys, handles backspace and returns the finished text in F$. Name is a free-text field. Age accepts digits only and is then range-checked. The Y/N question accepts nothing but Y or N.

10 DIM N$(12),F$(12),M$(30),B$(30)
20 OPEN #1,4,0,"K:":GRAPHICS 0:POKE 752,1
30 POSITION 2,1:PRINT "NEW MEMBER"
40 POSITION 2,4:PRINT "NAME:":POSITION 2,6:PRINT "AGE:"
50 POSITION 2,8:PRINT "JOIN CLUB (Y/N):"
60 GOSUB 2000:B$=" ":B$(30)=" ":B$(2)=B$
100 X=12:Y=4:L=12:M=0:GOSUB 1000:IF LEN(F$)=0 THEN 100
105 N$=F$
110 X=12:Y=6:L=3:M=1:GOSUB 1000:IF LEN(F$)=0 THEN 110
120 AGE=VAL(F$):IF AGE<1 OR AGE>120 THEN M$="AGE MUST BE 1 TO 120":GOSUB 1500:GOTO 110
130 POKE 764,255:POSITION 19,8:PRINT CHR$(160);:GOSUB 1400
140 POSITION 19,8:PRINT CHR$(K);
150 POSITION 2,12:PRINT "WELCOME, ";N$;" (AGE ";AGE;")"
160 CLOSE #1:END
1000 REM FIELD EDITOR: X,Y=POSITION L=MAX LENGTH M=1 DIGITS ONLY
1010 F$="":POSITION X,Y:FOR I=1 TO L:PRINT ".";:NEXT I
1020 P=LEN(F$):IF P<L THEN POSITION X+P,Y:PRINT CHR$(160);
1030 GET #1,K
1040 IF K=155 THEN 1200
1050 IF K=126 THEN 1300
1060 IF K>96 AND K<123 THEN K=K-32
1070 IF P=L THEN M$="FIELD IS FULL":GOSUB 1500:GOTO 1030
1080 IF M=1 AND (K<48 OR K>57) THEN M$="DIGITS ONLY, PLEASE":GOSUB 1500:GOTO 1030
1090 IF K<32 OR K>95 THEN 1030
1100 POSITION X+P,Y:PRINT CHR$(K);:F$(P+1)=CHR$(K):GOSUB 1550:GOTO 1020
1200 IF P<L THEN POSITION X+P,Y:PRINT ".";
1210 GOSUB 1550:RETURN
1300 IF P=0 THEN 1030
1310 POSITION X+P-1,Y:PRINT ".";:IF P<L THEN PRINT ".";
1320 IF P=1 THEN F$="":GOTO 1020
1330 F$=F$(1,P-1):GOTO 1020
1400 GET #1,K:IF K>96 THEN K=K-32
1410 IF K<>89 AND K<>78 THEN M$="PRESS Y OR N":GOSUB 1500:GOTO 1400
1420 GOSUB 1550:RETURN
1500 POSITION 2,20:PRINT M$;B$(1,30-LEN(M$));
1510 SOUND 0,200,12,8:FOR W=1 TO 30:NEXT W:SOUND 0,0,0,0:RETURN
1550 POSITION 2,20:PRINT B$(1,30);:RETURN
2000 B=PEEK(16):IF B>127 THEN B=B-128
2010 POKE 16,B:POKE 53774,B:RETURN

How it works:

  • Line 20 opens the keyboard as channel 1 for GET and hides the OS cursor with POKE 752,1. The field editor draws its own cursor, an inverse space (CHR$(160)), at the next free position (line 1020).
  • Lines 1040–1050 handle the editing keys. RETURN is ATASCII 155 and the backspace key (DELETE/BACK S) is 126. Backspace redraws dots over the deleted character and the old cursor, then shortens F$. A one-character string can't be cut down with F$(1,0), which raises ERROR 5, so line 1320 empties it directly.
  • Lines 1060–1090 are the validation. Lower case becomes upper case. A full field refuses more characters. A digits-only field refuses anything outside 48–57, and every field refuses control and graphics characters (codes below 32 or above 95). Each refusal prints a message on line 20 of the screen and buzzes. The next good key clears the message (line 1100).
  • Line 120 does the range check. Per-key checks can only ensure the field holds digits; whether 200 is a sensible age is decided after RETURN, and the field is simply asked for again.
  • Line 130 flushes any waiting key with POKE 764,255 before the yes/no question, for the reason given in section 3.

To add a field, print its label and call line 1000 with its position, length and mode. A useful extension is a mode 2 that accepts only letters and spaces, or a decimal mode that accepts digits and one full stop. Because the editor never uses INPUT, there is no ERROR 8 to trap, and the cursor can't wander off the form.

5. Menus

For menus, the same rule applies: accept only the keys that mean something. Reading the Keyboard shows the standard GET menu that rejects anything outside 1–3 before ON K-48 GOSUB. Two refinements: flush 764 before showing the menu, and if the menu has more than nine items, use letters (A=65) rather than two-digit numbers, so every choice is one keypress.

Troubleshooting

  • ERROR 8 at an INPUT. Letters or an empty line were entered for a number. Use one of the three methods in section 2.
  • ERROR 5 in a validation loop. You indexed into an empty string (I$(1,1) when LEN(I$)=0), or tried F$(1,0). Test the length first.
  • ERROR 129 when the program runs a second time. Channel 1 is still open from the last run. CLOSE #1 before ending, or put CLOSE #1 just before the OPEN.
  • ERROR 136 at an INPUT. The user pressed CTRL-3, the end-of-file key. TRAP it like any other error if that matters to you.
  • A yes/no question answers itself. A key was waiting in location 764. POKE 764,255 first.
  • BREAK still works after you disabled it. A GRAPHICS command, an OPEN of the screen, or the first PRINT after it switched it back on. Call the disable routine again after those.
  • The screen cleared when you opened E: on another channel. That is how the editor's OPEN works. Open it at the start of the program, before drawing.

Further reading

  • The Atari BASIC Source Book by Bill Wilkinson, Kathleen O'Brien and Paul Laughton (COMPUTE! Books, 1983), Chapter 13: exactly how INPUT and READ divide a line among variables.
  • Mapping the Atari by Ian Chadwick (revised edition, COMPUTE! Books, 1985): locations 16 (POKMSK), 752 (CRSINH), 764 (CH) and 53774 (IRQEN).
  • Atari BASIC Faster and Better by Carl M. Evans (IJG, 1983), Chapters 11 and 12, "Keyboard Trickery" and "Controlled Data Entry": single-key input, menus, positional input fields, dollar, date and time fields, and a scrolling input window. Evans argues that a program should lock out every key except the ones it expects.
  • On this site: Reading the Keyboard, Console Keys and HELP, Error Trapping in Atari BASIC and Atari BASIC String Handling and Tricks.

Credits