Qbasic Tutorial for beginners and children (Under Construction)


Getting user input at run-time

Two commands or functions that allow you to get user input at run-time are:

  • The INPUT statement
  • The INKEY$ function

The INPUT statement

The INPUT statement reads data from the user and stores it to a memory variable. The syntax is as follows:

INPUT [prompt] ; [variable]

or

INPUT [prompt] , [variable]

or

INPUT [variable]

Example:

Example 1: Asking the user for his nameFinding the sum of 2 numbers
Program Code

DIM yourname AS STRING

PRINT "Enter your name:";

INPUT yourname

PRINT "Hello"; yourname; ". Have a nice day!"

Output on Screen

Enter your name: Loulou

Hello Loulou. Have a nice day!

 

 

 

The lines:

PRINT "Enter your name:";

INPUT yourname

can also be written as:

INPUT "Enter your name:"; yourname

Personally I prefer to use the first option because it is easier to understand and because it is the standard procedure in most programming languages.

Example 2: Asking the user for his score in English and Math and calculating the sum
Program Code

DIM english AS INTEGER

DIM math AS INTEGER

DIM sum AS INTEGER

 

PRINT "Enter your score in English:";

INPUT english

PRINT "Enter your score in Math:";

INPUT math

sum = english + math

PRINT "The sum is:"; sum

Output on Screen

Enter your score in English: 65

Enter your score in Math: 59

The sum is: 124

 

 

 

[ Previous Index Next ]


pages viewed since January  19 2002