Computer Science Education | Programming Language Tutorials For Beginners

QBASIC TUTORIAL 6 - User Input

USER INPUT
The INPUT Command is used to gather input from the user. Here is the syntax of the input command:

INPUT “[text to user”]; [Variables] ‘ Questions mark added
Or
INPUT “[text to user”]; [Variables] ‘ No questions mark added
For instance:
INPUT “What is your Name”; Name$
Or
INPUT “what is your Age”, Age

When a semicolon (;) is used after the text output to the user, a question mark (?) and ‘space’ are added to the output, When a comma (,) is used no question mark is added.
If a string is specified (eg  ‘name$’) anything the user enters before pressing the ‘return’ keys will be accepted. If a numeric variable (eg ‘age’) is specified, the user must enter a number (if any non-numeric key is entered, the error message “Redo from start’ will be output and the INPUT command rerun).

Example 1:
               CLS  
      INPUT "Enter your name: ", Name$ 
      PRINT "Hello, "; Name$; ".  How are you today?"


Output:


Don't forget the comma (,) between "Enter your name: " and Name$. Remember to save your work. Run it by pressing <F5> key or by locating the Run command in the top menu of the screen. When it asks, type your name, then press the <Enter> key.  
 
What's in a "Name$"?  
Recall in our previous tutorials that we learned about Variables and the types of Data in QBasics, "Name$" is called a "variable". Variables hold letters and numbers. The dollar sign ($) means this variable can hold letters. These are called "string variables". Variables without a dollar sign can only hold numbers. We'll be seeing them soon. You can call your variables anything you want. Try going back through this program and changing every "Name$" to "Fred$". What happens when you run it? Another way to think of a variable is to imagine a small bucket with a name on it. Put "Name$" on it. This is the bucket's (variable's) name. Now take a piece of paper and write your name on it and drop it into the imaginary bucket. Now the variable Name$ has your name in it. Computer variables can only hold one piece of paper (one value) at a time. To be variable means that things can change. Try running the program again, but this time type in a friend's name (don't forget the <Enter> key). Sure enough, the message changes.

Example 2:

DIM FirstName AS STRING
DIM LastName AS STRING
CLS

PRINT
INPUT “Enter the First Name: “, FirstName
INPUT “Enter the Last Name: “, LastName


PRINT
PRINT “Welcome to QBasic Tutorial 6, Mr “; FirstName; “ ”; LastName
PRINT “Hope you are doing great Sir?”


Output:


Please note: in the PRINT command, the (;) function concatenates (joins) the contents of the strings variables with the text between the quotes (“ “). Note the use of spaces so that the final printed text reads properly.
If a numeric variables is specified within the PRINT command, an additional space is automatically added both and after the number

Example 3:
DIM Num1 AS INTEGER
DIM Num2 AS INTEGER
DIM Num3 AS INTEGER
DIM Num4 AS INTEGER

CLS

PRINT
INPUT “Enter the First Number: “, Num1
INPUT “Enter the Second Number: “, Num2

PRINT
PRINT Num1; “+”; Num2; “=”; Num1 + Num2

PRINT
INPUT “Enter the First Number: “, Num3
INPUT “Enter the Second Number: “, Num4

PRINT
PRINT Num3; “*”; Num4; “=”; Num3 * Num4


Output:


If you are having any problem with the codes, let us know in the comment box. Thanks

Share:

2 comments:

Popular Posts

Elitcode - Learning Start Here

Elitcode Blog Archive