Contents / Part 2 / Previous chapter / Next chapter / Index


11 Input

The previous section showed how to get information out of the computer and on to the screen. This section deals with getting things from the keyboard into the computer. When a program is running there will often be a need for it to request some information from the person at the keyboard.

10 PRINT "HOW OLD ARE YOU"

20 INPUT AGE

30 IF AGE<18 THEN PRINT "YOU ARE TOO YOUNG AT ";

40 IF AGE=18 THEN PRINT "CONGRATULATIONS ON BEING ";

50 IF AGE >18 THEN PRINT "YOU ARE PAST IT IF YOU ARE ";

70 PRINT ;AGE

>RUN

HOW OLD ARE YOU

?22

YOU ARE PAST IT IF YOU ARE 22

Line 20 of the above program prints a question mark on the screen and then takes in everything that is typed on the keyboard until RETURN is pressed. Line 20 says INPUT AGE so the computer is expecting a number since AGE is a numeric variable rather than a string variable (see section 9). If words are supplied instead of numbers then the computer assumes that the number is zero.

>RUN

HOW OLD ARE YOU

?I DON'T KNOW

YOU ARE TOO YOUNG AT 0

Because line 20 said INPUT AGE a number was expected. If you want to INPUT a string (word or group of words) then you must place a string variable (e.g. NAME$) on the input line.

10 PRINT "WHAT IS YOUR NAME"

20 INPUT NAME$

30 PRINT "HELLO ";NAME$;" HOW ARE YOU?"

>RUN

WHAT IS YOUR NAME

?JOHN

HELLO JOHN HOW ARE YOU?

You must follow the word INPUT with a numeric variable if you are expecting a number and with a string variable if you are expecting a string.

As you will have seen from the examples above you usually need to print a question on the screen to tell the person at the keyboard what you are waiting for. In the last example the question was "What is your name". Instead of placing this in a separate PRINT statement you can include the question on the INPUT statement.

20 INPUT "WHAT IS YOUR NAME ",NAME$

30 PRINT "HELLO ";NAME$;" NOW ARE YOU?"

>RUN

WHAT IS YOUR NAME ?SUSAN

HELLO SUSAN HOW ARE YOU?

Notice the punctuation between the question "What is your name" and the string variable NAME$. It is a comma. Notice also that the computer printed a question mark after the question when the program was run. It always prints a question mark on an INPUT statement if a comma is used to separate the question from the string variable. If you leave the comma out of the program the computer will leave the question mark out when the program is RUN.

20 INPUT "WHAT IS YOUR NAME " NAME$

30 PRINT "HELLO ";NAME$;" HOW ARE YOU?"

>RUN

WHAT IS YOUR NAME STEPHEN ALLEN

HELLO STEPHEN ALLEN HOW ARE YOU?

The INPUT statement, which we have explored above, requires that the user presses the RETURN key after he or she has entered the reply. Until the RETURN key is pressed the user can delete errors with the DELETE key or delete the whole entry so far with CTRL U.

Several inputs can be requested at one time. If you type

10 INPUT A,B

20 PRINT A,B

two numbers will be expected by the computer. They can either be typed in separated by commas, or both can be followed by RETURN.

The INPUT statement will ignore leading spaces and anything after a comma unless the reply is inside quotation marks.

10 INPUT A$

20 PRINT A$

>RUN

?ABC,DEF

ABC

The INPUT LINE statement can be used in the same way as INPUT, but it will accept everything that is typed, including leading spaces and commas. Replace line 10 by

10 INPUT LINE A$

>RUN

?ABC,DEF

ABC,DEF

Of course if you make the program

10 INPUT A$,B$

20 PRINT A$,B$

you will get

>RUN

ABC,DEF

ABC DEF

because now two different inputs are needed in line 10

Exit: BBC Microcomputer User Guide; Kasoft Typesetting; Archer


The BBC Microcomputer User Guide was written by John Coll and edited by David Allen for the British Broadcasting Corporation.

Optical character recognition and original formatting effort by Mark Usher.

HTML version maintained by: Kade "Archer" Hansson; e-mail: archer@dialix.com.au

Last updated: Monday 12th February 2001