R.T.Russell Home Page

Chapter 8 - Interacting with the User




The earlier examples, like our circle program, have an obvious flaw. In order to run them, users have to manually edit the code and put new values into the initial variables. For some reason, most computer users, even those who think they know quite a lot about computers, turn pale at the thought of actually programming and would rather pay to have someone else do it for them. Strange really, but it's kept me in employment for a long time. What we need is a way of allowing the user to enter the value, for example, of the radius of our circle. This way the poor dears will never have to soil their hands with CODE. The designers of BASIC have already thought of this. Try this:
INPUT A$
PRINT "You entered: ";A$
END
When run, the screen sits there with a question mark until you type something and press Enter. After this, execution continues as normal. Great stuff. Now we can modify our circle program to make it interactive:
REM Area of a circle
INPUT Radius
Area=PI*Radius^2
PRINT "The area of your circle is ";Area
END
I've updated line 3 to include the things we've already covered. It would be nice though to make the program tell the user what it's expecting. After all, even less than writing code, users don't like reading manuals. (Most programmers don't either, actually.) INPUT is another command like PRINT with lots of variations. For a start, you can put a text string to tell the user what the program is expecting:
INPUT "What is the radius"; Radius
If you leave out the semicolon ( ; ) BBC BASIC will not print the question mark, so you don't have to phrase your prompt as a question:
INPUT "Please enter the radius: " Radius
Also, it is possible to prompt for more than one variable at a time providing they are separated by commas:
INPUT "Enter the width and height " W,H
PRINT "Rectangle has an area of ";W*H;" m^2"
END
You can enter the values separated by commas or Enter. BASIC will keep prompting until it has enough data to fill the variables specified. It is also possible to split the prompt up:
INPUT "Enter the width " W " and height " H
Finally, although there is more in the online help, you can put your prompt anywhere on the screen using TAB:
INPUT TAB(5,5);"What is your name"; N$
PRINT TAB(5,6);"Hello, ";N$
END
INPUT LINE

There is a variation to INPUT that is worth mentioning at this point. By including the keyword LINE, we can make BASIC accept everything the user types and put it into one string variable. Ordinarily, INPUT will remove leading spaces and stop at the first comma after the initial text. By using INPUT LINE, we override this behaviour. To illustrate:

REM INPUT vs. INPUT LINE
INPUT "Enter your full address: " Address$
PRINT "Address with INPUT: ";Address$
INPUT LINE "Enter your full address: " Address$
PRINT "Address with INPUT LINE: ";Address$
END

It's very much horses for courses here, one variation is not better than another, it just depends what you need.

Tip: Entering numbers
When employing INPUT and INPUT LINE, the user has to be intelligent about his choice of input. If you're expecting a number and a string is entered, BASIC will return zero. A good way round this is to use a string to catch the entry then convert it to a number with VAL. Bearing this in mind INPUT will serve us as a good way to write interactive programs.

Exercises

1) The volume of a cylinder is PI*Radius^2*Length. Write a program that will ask for radius and length then give the volume.
2) Have a program prompt to enter your full name. Print the number of characters (including spaces) in the name.

Left CONTENTS

CHAPTER 9 Right


Best viewed with Any Browser Valid HTML 4.0!
© Peter Nairn 2006