Creating an Interactive Program


Using the Input command with the Print command

The PRINT command we used displayed pre-programmed information. We need to permit the operator to put variable information into the program using the keyboard. The INPUT command halts the program, permitting the operator to enter information into a storage space called a variable and the program uses this information to determine what need be displayed. The INPUT command along with the PRINT statement make a program Interactive. Here is an example of a simple program that permits the operator to provide text from the keyboard and the program simply sends this information back out to the display. A useless program, except to demonstrate the use of the two commands. Copy and paste the program into a plain text file (like NotePad.exe) and drag it into the GWBasic icon to run it. Be sure to put a line feed/ carriage return (RETURN or ENTER key) at the end of line 50, or the interpreter won't execute it. The RETURN character is necessary to terminate a line of code.

10 CLS
20 PRINT "Type in some text, and press ENTER"
30 INPUT A$
40 PRINT "The text that you typed in is:"
50 PRINT A$


Line 10
clears the screen.

Line 20
displays instructions for the operator

Line 30
halts the program permitting the operator to enter text.
The program continues on when the carriage reture (ENTER) key is pressed.
The text that was entered is stored into a variable named A$.
A is the variable and inclueds the $ to show that it is a text string.

Line 40
announces that the results of the INPUT will be displayed.

Line 50 PRINTs the contents of A$ to the display.

To RUN the program again, type RUN and ENTER. The sequence will repeat itself.


We used a string variable in our program. Its name was A$, indicating that it was a string variable. We use the INPUT statement to enter text data into it. Data can also be placed into a variable with the LET command. The let command such as:

10 LET A$= "hello "
20 LET B$= "world"
30 PRINT A$;B$

The LET statement is so common that it is assumed to be present in a variable assignment.

10 A$= "hello "
20 B$= "world"

will do exactly the same thing. There are some subtle cases where it is necessary to use the LET in the variable assignment command, but that will come later.

Variable may be named useing characters from A to Z and numbers 0 to 9, but the variable name must begin with a character A through Z. Example of valid variable names are:

A= 3
X= 5
A$= "variable"
NAME$= "Adriana"
B3= 21

If your coding assigns a numeric to a string variable or tries to assign an ascii string to a numeric variable, then the interpreter will give a TYPE error message.

A= "3" is illegal
A$= 3 is also illegal
These are examples of assigning a string to a numeric variable and attempting to assign a numeric value to an ascii text string variable. An error message results.
A variable assignment may be done either under program control as shown above or in the direct mode from the keyboard.



Here is an interactive program that simply adds two numbers using numeric variable A and B.
This program also illustrates some of the more complex forms of the INPUT and PRINT statements than were given earlier. Copy and paste it into a text file with a dot BAS extension and run it by dragging and droping it into the GWBasic icon. 10' This program simply sums the two numbers input
20' input by the operator
30 CLS
40 PRINT
50 PRINT "Please enter the two numbers to be added "
60 PRINT
70 INPUT "enter the first number ", A
80 INPUT "enter the second number ", B
90 PRINT
100 PRINT " "; A
110 PRINT " + "; B
120 PRINT " = "; A+B

Notice line 10 and 20. The have a hyphen after the line number. It is a remark line used only to document the program. This is for the programmer to document the program. These lines do not execute anything written after the hyphen.

Line 30 Clears the display.
Line 40 uses the PRINT statement to advance a line
Line 50 simply posts the instructions to the operator
Line 60 advances a line again

line 70 and 80 use an advanced form of the input statement.
That which is between the quotation marks is printed to the display.
The comma is necessary to seperate the text from the variable name

Line 90 is another line advance

Lines 100, 110 and 120 are advanced forms of the PRINT statement
The text between the quotation marks is posted
The semi-colon declares that no line feed occurs before the variable name
Line 120 prints out the value of A plus B because the + directive sums the two.

This simple program suggests that very complex operation may be carried out under program control with variable that are provided by an operator. The + directive may be replaced with the - (minus sign) or * (multiplication sign) or the / (division sign). Also, logs, trig functions may be used. Some very useful engineering and financial application programs can be easily written.



A little editor's liscence to speak


Very complex programs may be made to interact with an operator. There is a good deal of art in programming. The way the documentation and the codeing appear reflects the artist in the programmer. I like to keep my coding neat and easy to follow. No two programmers will write a program the same, even if the programs do exactly the same thing. House-keeping is not mandatory to write a program, but it surely makes modification and trouble shooting much easier. In many cases, the author of a program might be recognized by the style of his program. I will try to keep my programs neat and clean.
_________ Happy Hacking ___________