Copy Link
Add to Bookmark
Report

QuickBasicNews Issue 1: INPUT and LINE INPUT

eZine's profile picture
Published in 
QuickBasicNews
 · 10 Jan 2020

 
…ÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕª
∫ INPUT and LINE INPUT ∫
»ÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕº

A BASIC tutorial for either QBasic or QuickBasic

While many programs simply run and do their thing, most programs
will require some kind of input from the user of the program. This
input my be as simple as waiting for a key to be pressed before
program execution continues. At other times you will need some type of
data input that must be supplied by the user. Input statements and
functions allow this interaction between the user and the program.

[NOTE] If you are a new programmer please note that it is not
necessary when working in the environment to capitalize
Keywords such as PRINT INPUT, LINE INPUT etc. when
you enter them. (The editor will take care of this task
for you). We show them capitalized here and in our
discussions to highlight them.

Information that is input into the program may be input from a file
on a disk, or may be supplied by the user of the program. For the
purpose of this discussion we will concern ourselves with input being
supplied by the user at the keyboard.

{ THE INPUT STATEMENT }

The INPUT statement allows a program to work with changing variables
supplied by the user each time the program is run. When an INPUT
statement is encountered the program will stop executing at that line,
display a question mark on the screen, and wait for some value to be
typed in at the keyboard. The value typed in is then stored in the
variable name that was included with the INPUT statement.

The most basic format for the INPUT statement is:

INPUT variablename

In the example above you make up what you want to call the variablename.
As a general rule you should make the name meaningful. For example,
if you wanted to store a persons age in a variablename you might call
the variablename (personsage). This would be a meaningful variablename.
Storing the persons age in a variablename such as (xyw) would be allowed,
but wouldn't be very meaningful. Once the user has INPUT the value,
the value that they entered becomes stored into that variablename.
When a program reaches the line with an INPUT statement,
such as the one above, the program will display a question mark on the
screen and wait for the user to type in a value at the keyboard. This
particular form of the INPUT statement will require the user to press
the ENTER key after they have supplied the value. ( This form
of the INPUT statement does not know if the value will be one, two,
or even 20 digits long). The ENTER key signals the end of the user input.
When the user presses ENTER the value will be stored in the variablename
and the program will continue to execute the next statement line.

Here is an example of using the INPUT statement

CLS
INPUT yourage
daysold = yourage * 365
PRINT "You have lived approximately"; daysold; " days."
END

A run of this program would look like the following ( the number
in parentheses is the value supplied by the user )

? (30)
You have lived approximately 10950 days.

This type of an INPUT statement wouldn't be very user friendly. How
would the user of the program know what kind of value you wanted entered.
The user might know to enter something because of the question mark, but
he wouldn't have any idea of what he was supposed to enter. Adding a
PRINT statement could be used just prior to the INPUT statement to prompt
the user for the required type of information. In which case our sample
program might now be:

CLS
PRINT "Please enter your age"
INPUT yourage
daysold = yourage * 365
PRINT "You have lived approximately"; daysold; " days."
END

Running the program would now look like this ( Again keep in mind
that the number 30 is what was supplied by the user. The parentheses
would not actually be entered by the user.

Please enter you age
? (30)
You have lived approximately 10950 days.

Well, that's a little improvement. Yourage is a numeric variable that
we want the user to input. However, what would happen if the user
entered something other than just the number 30. For instance when
prompted for "Please enter you age" he might have entered:

I am 30 years old
thirty

Such replies would have caused the program to display the error
message (Redo From Start). So, what is wrong with the inputs
above. In the first case our simple INPUT statement was looking for a
numeric variable. "I am 30 years old" contains a number, but also
contains what are known as string characters. "thirty" may be a number
to you, but to the program it is just another meaningless string of
characters. One way to cope with this is to make it very clear to the
user just what type of information is being asked for, and in what
format you would like it. Thus you might do something like this:

CLS
PRINT "Please enter your age as a number such as 55"
INPUT yourage
daysold = yourage * 365
PRINT "You have lived approximately"; daysold; " days."
END

[NOTE: It is important to point out that another problem
can occur if the user includes a comma with the number in
response to an INPUT statement. For example a number such as
12,365 may be INPUT with the comma included. This would also
give us a (Redo From Start) error message. With INPUT, a number
should be input with no commas (Ex: 12365).

This brings up the next logical question. What if you had wanted
the user to input a string of characters rather than just a number?
In this case you would include a dollar sign after the variablename.
For example in the above examples where we wanted the person's age
as a numeric variable we used "INPUT yourage". Had we simply
wanted to ask for their name we might have used something like
(INPUT yourname$). The person then could have inputted a string of
characters ( such as the characters in a name ). The dollar sign
at the end of the variablename is what determines whether the
variablename is to be a number, or a string of characters.
BY the way, a number such as 30 can also be considered a string of
characters. If you had not intended to do math with that number, then
you could have just had it inputted as a string variable. Then it
would not have mattered if they had entered their age as "30" or "thirty".

There is a way to bring back a string variable such as "30" to
the program as a numeric variable should you need to. This is
done with the VAL function. As an example - Assume you had gotten
a persons age "30" and put it in a string called age$. Since it is
a string you couldn't do math with that string name directly.
However by using the following function you could take the string
"age$" and convert it into a numeric variable called "yourage"
this way:

yourage = VAL(age$)

After this statement was executed yourage would contain the number
30 and age$ would contain the string "30".

Now, let's get back to our original INPUT statement. Thus far
we have discussed the simplest form of the INPUT statement which
was INPUT variablename. The full format for the INPUT statement
is as follows:

INPUT [;] ["promptstring"] [, or ;] variablename

Whenever you see the format for a BASIC statement anything within
the Brackets is OPTIONAL information. First lets discuss the
optional PROMPTSTRING information. In our examples above we have been
using a PRINT statement to PROMPT the use with a message about the
information we wanted. By using the optional PROMPTSTRING parameter in
the INPUT statement we can include this prompt message from within the
INPUT statement, without having to use a separate PRINT statement. Here's
how our example program would look now using the optional PROMPTSTRING
option instead of the separate PRINT statement:

CLS
INPUT "Please enter your age as a number such as 55"; yourage
daysold = yourage * 365
PRINT "You have lived approximately"; daysold; " days."
END

With the expanded INPUT statement above we accomplish the same results
without the need for the additional PRINT statement. Please notice that
although our prompting message is a question, we did not include a
question mark in our prompting message. Doing so would have resulted in
two question marks on the screen, because INPUT automatically
includes one for us. Another thing to notice is that what we want as our
prompting message should be enclosed within quotation marks, and a
semicolon follows the prompt message just prior to the variablename.
Look at the format of the INPUT statement again:

INPUT [;] ["promptstring"] [, or ;] variablename

^ Here it says we may use a comma or
a semicolon before the variablename. The comma would allow for
multiple values to be INPUT using just one INPUT statement. For example
if we wanted three values input for the users age, their mothers age
and their dads age we might use three separate statements such as
this

CLS
PRINT "Please enter yourage, your mothers age and your dads age "
INPUT yourage
INPUT momsage
INPUT dadsage
END

However, by using the comma we could ask for all three of these
values on one line like this:

CLS
PRINT "Enter yourage, your mothers age and your dads age"
INPUT yourage, momsage, dadsage
END

Of course, we could have used the optional PROMPSTRING form
of the INPUT statement and included the actual prompt message on the
same line as the INPUT statement, something like this:

INPUT "Enter yourage, mothers age and dads age "; yourage, momsage, dadsage

Be careful asking for multiple INPUTS on one line like this. For this
form of the INPUT statement to work correctly the user would have to
know to enter one value, then enter a comma, then enter the second value,
then enter a comma, and finally enter the last value. For example -
a sample run of this program and the INPUTS from the user might look
like this:

Enter yourage, mothers age and dads age? (30,62,65)

The information in parentheses would have had to have been supplied
by the user, and they would have had to have included the commas.
There are times when you might want to use multiple inputs on one line.
Just consider if that's the way you want to do it.

Here is the format for the INPUT statement again:

INPUT [;] ["promptstring"] [, or ;] variablename

^ besides separating multiple INPUT items,
there may be times when you do not want the question mark to appear
on the screen when an INPUT statement occurs. An example of this
might be if you were asking for the person to choose from a list
containing items one through five. To eliminate the question mark
just use the comma instead of the semicolon:

CLS
INPUT "Select an item from 1 to 5", number
END

A run of the program would then look like this:

Select an item from 1 to 5 (2)

Before we discuss the final optional parameter let's discuss how we
might get a persons first and last name. We could certainly do something
like this (Using string variables):

CLS
INPUT "What is your Firstname"; firstname$
INPUT "What is your Lastname"; lastname$
END

This program would produce the following output (Assuming you
supplied what is in Parentheses when your were prompted for the INPUT.

What is your Firstname? (John)
What is your Lastname? (Doe)

We could get both ( The prompt messages and the names) INPUT on the
same line by using another optional form of the INPUT statement:

INPUT [;] ["promptstring"] [, or ;] variablename

^ This optional semicolon when used after INPUT, but
before the promptstring will suppress the cursor from moving down
to the next line. Now with a slight modification to the above program
we can do this:

CLS
INPUT ; "What is your Firstname"; firstname$
INPUT " What is your Lastname"; lastname$
END

Will produce the following output:

What is your Firstname? (John) What is your Lastname? (Doe)

[Note: notice how we included a couple of spaces between the
first quotation mark and the word (What) in the second INPUT
statement. Try what happens if you don't include these spaces.]

First the program would have prompted you for your Firstname. Then,
after you enter (John) without the parentheses of course, the next
prompt would occur on the same line ( What is your Lastname?). Then it
would wait for your INPUT of (Doe).

One final note about using String inputs. If the user might include
a comma as part of the string of information you cannot use the INPUT
command. A prime example of where a string of information might include
a comma would be the following:

CLS
INPUT "Where are you calling from"; where$
END

In this example someone might INPUT the answer ( Nashville, TN ).
The reason that a comma cannot be used is because a comma is known as
a delimiter as far as the INPUT command is concerned. It is called
a delimiter, because it delimits ( separates ) the values entered from
each other. Therefore, if you entered INPUT with a comma it would try
to interpret that as two different inputs being entered when you only
asked for one. This would create an error. The way to get around it
is with the LINE INPUT statement discussed next.

{ Using LINE INPUT }

The LINE INPUT statement will allow for a string input containing
commas. LINE INPUT requires that the data that is input must be a
string variable. The format for the LINE INPUT statement is:

LINE INPUT [;] ["promptstring";] stringname

Remember how the INPUT statement would include a question mark
automatically after the promptstring. Well, LINE INPUT does not
put the question mark there for you. Therefore, if you want the
question mark included in the prompt, just include it as part of
the prompt string

CLS
LINE INPUT "Where are you calling from?"; where$
END

Now they could include something like (Nashville, TN.) as the
INPUT information. Just remember, LINE INPUT allows delimiters such
as a comma to be input as part of the string of information. The
length of the string may be up to 255 characters.

← previous
next →
loading
sending ...
New to Neperos ? Sign Up for free
download Neperos App from Google Play
install Neperos as PWA

Let's discover also

Recent Articles

Recent Comments

Neperos cookies
This website uses cookies to store your preferences and improve the service. Cookies authorization will allow me and / or my partners to process personal data such as browsing behaviour.

By pressing OK you agree to the Terms of Service and acknowledge the Privacy Policy

By pressing REJECT you will be able to continue to use Neperos (like read articles or write comments) but some important cookies will not be set. This may affect certain features and functions of the platform.
OK
REJECT