Getting (and saving) input from the user
Another method of assigning a value to a variable is to use it to save a value that the users have inputted through a keyboard. The process of obtaining information from the user has three steps.
- Declare a variable that will store the value that the user enters
- Output instructions to the user to tell them what value to enter
- Assign the value obtained into a variable that you specify
Declare the variable Declare the variable that will hold the input entered by the user
Create the Output statement Click on the vertical line where you would like to place an output statement and select the output shape.
- Enter the instructions for the user (make sure to put the instructions inside quotes)
- Click ok
- Assign the input value in a variable
Click on the vertical line again (just below the output shape) and choose Input
- Double-lick the input shape
- Enter the name of the variable you declared in step 1
Using user input in a calculation and displaying the result
Often you will use user input in calculations that your program performs. The basic steps for involved in this process are:
- Get a value from the user and store it an appropriate variable
- Use the variables to do any calculations necessary
- Display the end result to the user
Take a look at the program below. It has the following functionality:
- Declare a variable to store user input
- Declare a variable to store result of calculation
- Output the result of the calculation