Beginners: User-Friendly Programs

Publication: Syntax
Pub Details: Syntax/Syntax ZX80 v2 n10 (Volume 2, Issue 10)
Date: October 1981
Pages: 13-14

Experienced programmers know that the best programs are not necessarily those with the most clever structure. Good programs do the required job and make it easy for the user. Programs that are simple to run and understand the results of are “user-friendly.” This month we’ll look at some easy programming methods to write user-friendly programs for either 4K or 8K ROM programs.

Almost all programs require some input from the user to run. You may have to enter numbers or other information. When you run a program on the ZX80, the cursor indicates when the machine expects input–LS for numbers (or numerical variable) and “L” for strings (usually letters) on 4K machines, L for numbers and “L” for strings on 8K machines. A good program gives prompts for the user; it tells you what information it needs.

Prompts are simply PRINT statements asking for information. Just use a line like:

  10 PRINT "WHAT IS YOUR NAME?"

The next line tells the computer to accept input from the keyboard:

  20 INPUT A$

INPUT A$ means that the user will enter a literal string, so each time it prints A$, it will print just what you type in. String variables consist of any single letter, A-Z, followed by a dollar sign.

Now add:

  30 PRINT "HELLO, ";A$

and the computer calls the user by name–how much friendlier can you get?

This method also works for entering numbers, except that the input is not a string but a numerical variable:

  10 PRINT "ENTER AMOUNT"
20 INPUT A

Numerical variables can be any length, composed of letters A-Z and digits 0-9.

After inputting a number, another nice touch is to print the number for the user to see.

  30 PRINT A

or:

  30 PRINT "AMOUNT=";A

To remove the prompt, insert:

  25 CLS

before the second print statement so the user sees only his response displayed on the screen.

For users not experienced in handling computers, programs that allow you to easily exit or rerun a program are helpful. Add these lines to the end of your program, using appropriate line numbers. At the end of a game, for example, add:

 500 PRINT "PLAY AGAIN? Y OR N"

This line gives the user the option of running the program again and tells him how to respond. Again, follow this with an INPUT line (a string variable because the answer will be a letter):

 510 INPUT Z$

Now you tell the computer how to interpret the user’s response:

 520 IF Z$="Y" THEN GO TO (first line number)
530 IF Z$="N" THEN STOP

Another way to run the program again is to use the RUN command:

 520 IF A$="Y" THEN RUN

This method clears any variables the user entered during the first run. It’s just like typing RUN from the edit mode (when the listing is on the screen). It’s a good idea to put a CLS at the front of the program to remove any leftover display from the last run.

Another elegant touch to a BASIC program is adding tests for valid inputs. For example, if the program asks for a number between 1 and 10, you don’t want it to continue if the user enters 500. Here’s one way to test inputs.

First, of course, insert a prompt to ask for the desired input and the input command for the computer:

  10 PRINT "CHOOSE A NUMBER BETWEEN 1 AND 10"
20 INPUT X

Now for the test:

  30 IF NOT X*2<21 AND X*2>0 THEN GO TO 10

Any number between 1 and 10 multiplied by 2 will be less than 21 (the largest acceptable number, 10, times 2 is 20.) And any number greater than 1 is positive, so multiplying it by 2 will be greater than 0. If both of these are true, the number must be 1 or 10 or a number between the two. If both are not true, the computer goes back to line 10 and again asks the user to input a number.

To test a string variable for validity, use the same method:

 500 PRINT "PLAY AGAIN? Y OR N"
510 INPUT Z$
520 IF NOT Z$="Y" OR Z$="N" THEN GO TO 500

Line 520 tests to see if the user’s response is either a Y or an N. This time Z$ can be either of two choices, so you use OR. Since the response is not valid only if it isn’t a Y or an N, you use IF NOT. If the response isn’t valid, the computer goes back to line 500, which asks the question again until it gets an answer it expects.

These methods are not the only ways to accomplish their goals, but they do work. If you have enough RAM to fit them in your program, using them will make it easier for others to use your program.