.                                                . How to use this forum              The QBasic Forum                                     

 Return to Index  

2- INPUT statements and string concatenation

June 14 2004 at 11:19 AM
Solitaire  (Login Solitaire1)


The INPUT keyword/command is used for getting information from the user. It is a way for the user to assign a value to a variable. The last thing on an INPUT line must be the name of the variable. The screen will display a question mark right after the INPUT statement and wait for the user to type something and press the Enter key. Example:

PRINT "Enter a number after the ?"
INPUT num1
INPUT num2
num3 = num1 + num2
PRINT "The total of your two numbers is"; num3

It is possible for the user to press the Enter key without typing a number. In that case, the value 0 will be assigned to the number variable. However, if the user types a letter instead of a number, QBasic will issue an error statement, "Redo from start" which means it was expecting a number and the input was wrong. This will not happen if the variable is a string. Example:

PRINT "Enter your name after the ?"
INPUT nom$
PRINT "Your name is "; nom$

You may combine a print statement with an input statement. Example:

INPUT "What is your name"; nom$

When you use a semicolon, it will display a question mark. If you don't want a question mark to show, use a comma instead of the semicolon. Example:

INPUT "Enter your name: ", nom$

Variables may be assigned with the = assignment operator and then changed with an INPUT statement, and the other way around. Example:

xx = 25
PRINT "xx is"; xx
INPUT "Enter a new number: ", xx
PRINT "xx is now"; xx
xx = 25
PRINT "xx was changed back to"; xx


Strings may be concatenated using the + concatenation operator. Example:

INPUT "Enter a color: ", col$
toy$ = col$ + " " + "ball"
PRINT "You have a "; toy$

Notice you need to place a space between the two strings when combining them.

---------------------------LAB PROBLEM--------------------------

Write a program that asks the user to enter his first name, last name, and his age. Concatenate the two names into a new string variable for the full name. Print out a greeting using the full name. Then tell the user how old he is now and how old he will be 5 years from now.

 Respond to this message   
 Copyright © 1999-2018 Network54. All rights reserved.   Terms of Use   Privacy Statement