Suite101

Text Input with BBC BASIC

How to retrieve user input in the command line environment

© Guy Lecky-Thompson

Using INPUT, Lecky-Thompson.net
In this article we learn how to provide for user input in a BASIC program, using the INPUT keyword.

The INPUT Keyword in BBC BASIC

The INPUT keyword in BBC BASIC is used to provide a place for the user of the program to input text from the keyboard. Input starts with the first character that they type, and ends with a return character (new line). Usually, the input value is assigned to a variable, when specified as part of the command:

  INPUT szTextIn$

However, it is possible to omit the variable, and BBC BASIC will not complain.

Generic usage of the INPUT keyword in BBC BASIC follows the same principles as the PRINT keyword that we looked at in the Text Output with BBC BASIC article.

If there is printable text (in quotes) preceding the variable name, then it will be output before the user input. An example of this might be:

  INPUT "What is your name : " szName$

However, there will be no question mark (?) output at the end of the phrase, so the actual screen output will look like:

  What is your name :

If the text is omitted, then only a question mark is printed:

  INPUT szName$

The above will yield a single line, with a question mark on it, which is not terribly user friendly. A better approach is to use either a PRINT and INPUT statement, or include the question mark in the quoted text. The following two lines are functionally equivalent:

  PRINT "What is your name " : INPUT szName$
  INPUT "What is your name ? " szName$

However, if a comma is introduced between the string and the variable name, then the question mark is printed, as one might expect.

Controlling Placement of User Input

As mentioned, we can use TAB and SPC to control the placement of the user input on the screen. For example, if we could use a command such as the following:

  INPUT SPC(10); "Name ", szName$

The above snippet would output enough spaces such that the string Name would start after the first 10 characters of the screen. The same effect can be produced using the TAB keyword, which was also covered in the PRINT tutorial.

Tricks with EVAL

We can also use EVAL to swap between user input values. For example, we might use code such as:

  INPUT szNumber$
  nNumber = EVAL (szNumber$)

We shall look at EVAL in more detail at a later date, however, it is important to explain that it EVALuates whatever string is passed to it. So, in this case, it is just converting a string t a number - the target variable being numerical in nature.

However, EVAL can also be used to do little calculations. If the reader enters the following code in immediate mode, or as a program in BBC BASIC, they will be able to evaluate any expression that includes numbers or functions.

   INPUT szInString$ : PRINT EVAL(szInString$)

The reader is invited to try putting in 2+2, SIN(3.141) or even a combination, and EVAL will evaluate the expression faithfully. In a program, it is more usual to create a string from several variables to make an EVALuatable expression. Consider the following code:

  INPUT "First Number:", n1
  INPUT "Second Number:", n2
  INPUT "Operator:", szOp$
  PRINT EVAL(STR$(n1)+szOp$+STR$(n2))

The reader is invited to think what the result of the PRINT statement might be if we entered '42', '64' and '*' as the three variable values.

BBC BASIC Tutorial Links


The copyright of the article Text Input with BBC BASIC in Computer Programming Tutorials is owned by Guy Lecky-Thompson. Permission to republish Text Input with BBC BASIC in print or online must be granted by the author in writing.





Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo