Classic Computer Magazine Archive COMPUTE! ISSUE 88 / SEPTEMBER 1987 / PAGE 64

The Beginner's Page

C. Regena

Interactive Programming

Many people start programming by writing a demonstration program—type RUN and the computer performs. The beginning programmer may write PRINT statements that leave a message for the person running the program, or create a graphics or music demo to show off the computer's capabilities. The next step is to write an interactive program—one that involves the user.

An interactive program asks a question, and the user either types an answer or presses a key to answer. The computer then acts upon that input. The command most commonly used for interaction is INPUT, and there are many forms of the INPUT statement. We'll cover the general forms in this column, and then you can experiment and see what will actually work on your particular computer.

When INPUT is used, the computer waits for the user to type something and then press the ENTER or RETURN key. (To simplify matters, I'll just refer to the key as RETURN—your computer may call it the ENTER key instead.) Some computers automatically print a question mark, or a question mark and a space, and then show the cursor to indicate the user's turn to do something. Two sample statements are:

200 INPUT N
300 INPUT S$

Line 200 asks for a number, so whatever value the user types will be assigned to the numeric variable N. Line 300 asks for a string; the user's input will be assigned to the string variable S$. A string variable can contain alphabetic or numeric characters and symbols, or even a null string (entered by pressing RETURN without typing anything else), but a numeric variable must be a number only—the computer will print an error message if a string is entered.

The previous example lines will cause the program to wait for the user to enter something. Notice that nothing else happens until the RETURN key is pressed. When writing a program, you'll need to tell the user what you want. You can use PRINT statements to print a message and then use the INPUT statement to receive the answer. For example:

100 PRINT "TYPE A NUMBER"
110 INPUT N
200 PRINT "WHAT IS YOUR NAME?"
210 INPUT NAME$

Another form of using the INPUT command is to combine the PRINT and the INPUT into one statement using an input prompt. After the INPUT command, put in quotation marks the prompting message you want to print. Follow the last quotation mark with a semicolon and then the variable name:

100 INPUT "WHAT IS THE ANSWER";A
200 INPUT "WHAT IS YOUR NAME"
;NAME$

This method keeps the input cursor on the same line as the prompt message. The printing method puts the input on the next line. You can also print a message, put a semicolon after the printed message, and then use an INPUT command.

100 PRINT "ENTER THE MONTH.";
110 INPUT MONTH$

The version of BASIC for Atari eight-bit computers (those other than the ST line) does not allow an input prompt with the INPUT statement. Users of this dialect must always use the PRINT/INPUT combination.

Notice that INPUT normally prints a question mark to indicate that it is waiting for input. Some versions of BASIC allow you to suppress the question mark by using a comma instead of the semicolon after the input-prompt message:

200 INPUT "THE ANSWER IS", A

Notice that I included a space before the last quotation mark to get a space before the input cursor. You can experiment with input prompts to get the proper spacing and to understand the differences between using a semicolon and a colon. (The IBM, Amiga, and Atari ST versions of BASIC allow this format; the Commodore, Atari eight-bit, and Applesoft versions do not.)

You may ask for more than one variable in a single INPUT statement, but the variable names must be separated by commas. In this case, the user must enter the numbers or strings in the proper order, separated by commas:

200 INPUT "ENTER LAST NAME,
FIRST NAME, CODE" ; L$, F$, C

The user must enter three items separated by commas such as SMITH,CINDY,456.

Using more than one variable in the INPUT statement can cause confusion, because a user who is unfamiliar with the program may not know exactly what is expected and may not use the commas properly. The error message in this case may also be confusing. Some computers use?REDO FROM START to indicate insufficient input; you're then required to enter all items again. Other versions indicate that additional input is needed, in which case values or strings are accepted for the rest of the input items. The Commodore and Applesoft versions of BASIC print a pair of question marks to request additional input; BASIC for the Atari eight-bit models uses a single question mark.

To avoid problems, I prefer using a different INPUT statement for each item desired.

If you provide more values than the input requests, most versions of BASIC provide an error message to indicate this (?EXTRA IGNORED, for example). However, unlike other BASIC errors, this one does not halt program execution in most dialects.

The use of the comma as a separator imposes a restriction on what can be typed in response to an INPUT request. Specifically, your input cannot contain a comma. Some versions of BASIC also will not allow a colon (:) in the input line.

The following short interactive program illustrates different ways to use INPUT. Line 30 asks a question, and then line 40 receives an answer and puts it into the string variable N$. Line 50 uses that variable N$ in the PRINT statement. Line 70 asks a question requiring a numeric answer A. Line 80 then uses A in the printed response. Lines 100–140 illustrate one way of receiving input to add two numbers. The numbers are entered individually.

Line 30 uses a PRINT statement to ask for the input, and then line 40 uses INPUT alone. Line 70 uses an input prompt with a semicolon so the question mark is printed automatically. Lines 110 and 120 suppress the question mark by using the comma after an input prompt. You may find it necessary to modify this program to meet the restrictions of the INPUT statement in your dialect of BASIC.

30 PRINT "WHAT IS YOUR NAME?"
40 INPUT N$
50 PRINT "HELLO,";N$
60 PRINT
70 INPUT "HOW OLD ARE YOU";A
80 PRINT A;"IS A GOOD AGE."
90 PRINT
100 PRINT "NOW ADD TWO NUMBERS."
110 INPUT "FIRST NUMBER IS",N1
120 INPUT "SECOND NUMBER IS",N2
130 PRINT
140 PRINT "THE SUM IS";N1 + N2
150 END

INPUT receives whatever the user types in before pressing the RETURN key—whether it is one character, several lines of characters, or nothing. With INPUT, it is easy for the user to cause errors by entering something the program is not expecting. The result can be error messages or program crashes. You do need to be careful when using the INPUT statement. Try to be as specific as possible in asking for the input items. Next month, we'll discuss other methods of interactive programming that help the user to avoid INPUT errors.