QBasic Lesson 6: Inputting Values (9)

advertisement
116093834
QBasic Lesson 6: Inputting Values (9)
OBJECTIVES:
 Use the INPUT statement
 Use the LINE INPUT statement
The INPUT Statement
The INPUT statement format
INPUT var1, var2
The purpose of the INPUT statement is to elicit one or more values from the person at the
keyboard.
When your program reaches the INPUT statement, it displays a question mark and waits for the
user to type one or more values.
If one variable follows INPUT, the program expects only one value.
If more than one variable follows INPUT, the user must type values separated by commas until
each of the variables is filled.
Pressing Enter after typing values in response to INPUT informs QBasic that the user is finished
typing values into the INPUT variables.
Filling Variables with Values
When you use the INPUT statement, the values that go into the INPUT variables are not known
until the program runs and the user enters a value.
Only one question mark appears when the program reaches the INPUT statement even is there are
several variables after the INPUT statement that needs to be filled.
Example:
‘ get three numbers for the user
INPUT num1, num2, num3
Output:
?
You have to input three numbers separated by commas, because that is the format of this particular
INPUT.
116093834
The keyboard input must match in number and in type the variables that follow the INPUT
statement in the program or you will be asked to: Redo from start
Improving the Use of INPUT
If the users only see one question mark, they have no idea what kind or how many values to type.
You should always prompt users for values that they should type in response to INPUT.
Example:
CLS
‘prompt the user to input three numbers
PRINT “Please ENTER three numbers, separated by commas”
‘ get three numbers for the user
INPUT num1, num2, num3
‘Calculate the average
avg = (num1 + num2 + num3) / 3
‘print the results
PRINT “The average of your three numbers is: “; avg
Output:
Please ENTER three numbers, separated by commas
? 42, 67, 94
The average of your three numbers is: 67.6666
Press any key to continue
If you want the user to answer a question inside the prompt, two question marks appear.
Example:
116093834
PRINT “What is your name?”
INPUT fullName$
Output:
What is your name?
?
If you put a semicolon at the end of a prompt message, the next INPUT prints its question mark
directly to the right of the question. Leave the question mark out of the prompt and it will be
replaced by the INPUT question mark.
Example:
PRINT “What is your name”;
INPUT fullName$
Output:
What is your name?
Prompting with INPUT
If the program tells users exactly how to type the input, they will be more likely to match INPUT ‘s
expected variables in type and number.
You do not have to pair a PRINT statement with every INPUT. You can print the prompt directly
in the INPUT statement.
The format of the INPUT statement
INPUT promptString; var1, var2
116093834
Example:
CLS
‘prompt the user to input three numbers
INPUT “Please ENTER three numbers, separated by commas”; num1, num2, num3
‘Calculate the average
avg = (num1 + num2 + num3) / 3
‘print the results
PRINT “The average of your three numbers is: “; avg
Output:
Please ENTER three numbers, separated by commas? 42, 67, 94
The average of your three numbers is: 67.6666
Press any key to continue
INPUT statement can be used to pause the program and wait for the user to press a key on the
keyboard before continuing.
Example:
PRINT
PRINT “Press ENTER when you want to proceed with the program . . ”
INPUT ent$
‘ent$ is a variable that stores the keystroke of pressing ENTER
Inputting Strings
There is one character on the keyboard that you can not input in answer to an INPUT statement:
the comma.
This is because the comma is the delimiter that separates values in the INPUT list. Therefore, you
must put quotation marks around INPUT strings that contain commas. The quotation marks are
not part of the input value; they serve to enclose the full string, including the commas.
Example:
116093834
CLS
PRINT “Type a book’s title and enclose it inside double quotes.”
INPUT “What is the name of the book”; bookTitle$
‘print the results
PRINT “The title you entered is: “; bookTitle$
Output:
Type a book’s title and enclose it inside double quotes.
What is the name of the book? “To Err, To Live”
The title you entered is: To Err, To Live
Press any key to continue
Eliminating the Question Mark
If you follow the prompt string in the INPUT statement with a comma instead of a semicolon, no
question mark appears. The comma suppresses the question mark and the value entered appears
directly to the right of the prompt message.
The LINE INPUT Statement (not in the textbook)
The LINE INPUT statement lets users input strings that contain commas without having to enclose
the strings in quotation marks. It even allows input that contains quotation marks as part of the
string.
The format of the LINE INPUT statement
LINE INPUT [prompt message;] stringvariable
Differences between LINE INPUT and INPUT
LINE INPUT
 accepts string variables, not numeric variables, as input.
 You can enter only one string variable; you cannot list several variables after LINE INPUT
116093834


Does not automatically display a question mark; you must add a question mark at the end of
your message
Best used for strings that may contain characters that INPUT does not handle well; commas
and quotation marks
LINE INPUT and INPUT Cursor Control
If you put a semicolon immediately after INPUT or LINE INPUT, the cursor remains in the same
line as the input prompt.
Example:
INPUT “What is your name”; fullName$
By typing your name and pressing Enter, QBasic places the cursor on the next line.
Example:
INPUT ; “What is your name”; fullName$
Here after typing your name and pressing Enter, QBasic leaves the cursor in the space right after the
last character in your name.
Lesson 6 Review Questions
1. Which of the following statements always produces a questions marks?
a. LINE INPUT
b. PRINT
c. LET
d. INPUT
2. Why is the prompt message important when you use INPUT and LINE INPUT?
3. True or False: You can enter more than one variable value with INPUT.
4. True or False: You can enter more than one variable value with LINE INPUT.
5. How many question marks are produced by the following two lines of code?
INPUT “How old are you?”; age
INPUT “What is your name?” , fullName$
6. How many values does the following INPUT statement require? What are their types?
INPUT a, b$, c, d$
116093834
7. What, if anything, is wrong with the following LINE INPUT statement?
LINE INPUT “Please enter your city and state”; city$, st$
8. How could you enter the address, 8109 East 15th St. , Apt.6 ,with the following INPUT
statement?
INPUT address$
9. What error message appears if you enter three numbers for the following INPUT?
INPUT “Enter your sales and net sales”; sal, netSal
10. What error message appears if you enter two numbers for the following INPUT?
INPUT “Enter the three highest grades”; g1, g2, g3
116093834
Lessson 6: Inputting Values
Exercises
For each of the following programs make sure you use remarks and clear the screen where
necessary. Save a copy of each to your home folder. Turn in a hardcopy of each programs print
out that includes your name on the first line, the program name on the second line and followed by
four blank lines before the print out of the program appears. Also turn in a hardcopy of the program
code and the code on disk.
1. Write an INPUT statement that prompts users for their names and weights, stores the names
and weights in the appropriate variables, and keeps the cursor on the same line.
2. Suppose that you are a teacher who needs to average the grades of 10 students. Write a
program that prompts you for 10 different grades and then displays an average.
3. Modify the program in the preceding exercise to ask for each student’s name as well as the
grade that the student is in. Print the grade list to the printer, with each student’s name and
grade appearing in two columns. At the bottom of the report, print the average of the
grades.
Hint: Store the 10 names and 10 grades in different variables, such as name1$,
grade1, name2$, grade2 and so on. This program is easy to write but take almost 30 lines of
code, plus appropriate remarks. Later, you will learn ways to streamline this program.
4. Write a program that prompts the user for the number of hours worked, the hourly rate, and
the tax rate, and then displays the taxes and net pay.
5. Write a program that prompts the user for a full name, hours worked, hourly rate, and tax
rate. Compute the taxes and net pay, and then print a check to the user on the printer.
6. Write a child’s math program so that a child can check their answer after adding subtracting,
multiplying, and dividing two numbers. Prompt the child to input two numbers and then
pause the program until the child is ready to check their answers. Then print to the screen
the two input numbers, the math operation and the correct answer.
Download