Getting Input from the User
Page Contents
Getting user input is a basic part of almost all programs. It provides a way of changing variables or transferring control to the user. The four commands used for getting input are: Prompt
/Input
, getKey
, and GetCalc
.
User input includes getting values for variables on the calculator, getting the keys that the user pressed, and getting a variable off of or sending a variable to another calculator over a link cable.
Getting Input
You can get input with either the Prompt
or Input
command. There are certain advantages and disadvantages to each command, and there are also certain situations where each command should be used. Because these two commands only work on the homescreen, you will have to make your own input function using the getKey
command if you want to get input on the graphscreen.
Prompt
and Input
can be used with any variable, but some of the variables have to be entered in a certain way. If the variable is a string, the user must put quotes ("") around the value. The user must also put curly braces ({}) around lists and square brackets ([]) around matrices.
Getting Input with Prompt
The Prompt
command is the simplest way of getting user input. The Prompt
command asks the user to enter a value for a variable, waiting until the user enters a value and then presses ENTER. When using Prompt
, the variable that is being asked for will be displayed on the screen with an equal sign and question mark (=?) after it.
Format
:Prompt variable
Because displaying what variable the value will be stored to does not tell the user what the variable will be used for, you can put a Disp
command before the Prompt
command to give the user some more insight into what an appropriate value for the variable would be. The Prompt command will be displayed one line lower, though, because the Disp
command automatically creates a new line.
Format
:Disp "Text"
:Prompt variable
When you have a list of Prompt
commands (and each one has its own variable), you can just use the first Prompt
command and combine the rest of the other Prompt
commands with it. You remove the Prompt
commands and combine the arguments, separating each argument with a comma. The arguments can be composed of whatever combination of variables is desired.
The advantages of combining Prompt
commands are that it makes scrolling through code faster, and it is more compact (i.e. smaller) and easier to write than using the individual Prompt
commands. The primary disadvantage is that it is easier to accidently erase a Prompt
command with multiple arguments.
:Prompt A
:Prompt Str1
Combine the Prompts
:Prompt A,Str1
To use the Prompt
command, you should first be in the Program editor for your program. In the Program editor, press the PRGM button, then arrow over to the I/O menu. Then, scroll down to Prompt
and press ENTER. Now the Prompt
command has been put into your program. You then have to type what variable(s) you want to prompt the user for (separating each one with a comma).
Getting Input with Input
The other way to get input is to use the Input
command. The Input
command asks the user to enter a value for a variable (only one variable can be inputted at a time), waiting until the user enters a value and then presses ENTER. The Input
command does not display what variable the user is being asked for, but instead just displays a question mark.
Format
:Input variable
Because just displaying a question mark on the screen does not really tell the user what to enter for input or what the input will be used for, the Input
command has an optional text message that can be either text or a string variable that will be displayed alongside the input.
Only the first sixteen characters of the text message will be shown on the screen (because of the screen dimensions), so the text message should be kept as short as possible (a good goal is twelve characters or less). This is so the value the user inputs can fit on the same line as the text. In the case that the value is too long, it will wrap around to the next line.
Format
:Input "Text",variable
:Input Str#,variable
If the text message is longer than twelve characters or you want to give the user plenty of space to enter a value, you can put a Disp
command before the Input
command. You break the text message up and display it in parts. The Input
command will be displayed one line lower, though, because the Disp
command automatically creates a new line.
Format
:Disp "Text"
:Input "Text",variable
When you are just using the text message to tell the user what the variable being stored to is, you should use the Prompt
command instead. And, if there is a list of Input
commands following the same pattern, you can reduce them to just one Prompt
command.
:Input "A",A
:Input "B",B
Replace with Prompt
:Prompt A,B
The Input
command can also be used another way. When you just put the Input
command by itself, the graphscreen will be shown and the user can move the cursor around. When the user presses ENTER, the (x,y) coordinates of the cursor will be stored to the X
and Y
variables, respectively.
Format
:Input
To use the Input
command, you should first be in the Program editor for your program. In the Program editor, press the PRGM button, then arrow over to the I/O menu. Then, scroll down to Input
and press ENTER. Now the Input
command has been put into your program. You then have to type the text message and the variable that you want to ask the user for.
Reading Keypresses
The getKey
command is widely used in most programs because it allows a program to transfer control to the user. The getKey
command tells the calculator to check to see if a key was pressed. And, if a key was pressed, the key number is returned.
Every key has a number assigned to it, except for ON (which is used for breaking out of programs). The numbering system consists of two parts: the row and column. The rows go from one to ten, starting at the top; and the columns go from one to six, starting from the left. You just put the row and column together to find the key's number.
The getKey
command doesn't pause a program, waiting for the user to press a key. If a key isn't pressed at the exact time the calculator executes it, the calculator returns zero and moves on to the next command. Because of this, the getKey
command is usually used inside loops and stored to a variable. Storing getKey
to a variable allows the program to keep track of which key was pressed, taking different actions depending on what the key was.
Example
:Repeat K
:getKey→K
:End
You can also put getKey
in the condition of a loop, to make the loop repeat until any key or a particular key is pressed by the user. The same thing can be done with conditionals as well. This is useful if you don't want to store getKey
to a variable, but you still want to have the user press a key.
Example
:Repeat getKey
:End
To use the getKey
command, you should first be in the Program editor for your program. In the Program editor, press the PRGM button, then arrow over to the I/O menu. Then, scroll down to getKey
and press ENTER. Now the getKey
command has been put into your program.
Linking Calculators
The GetCalc
command allows you to make multiplayer games, where two calculators communicate with each other across a link cable that is connected between them. The GetCalc
command can send or receive a variable from another calculator. The variable can be any variable, and when it is sent, it is stored to the variable of the same name on the receiving calculator.
Format
:GetCalc(variable)
For the GetCalc
command to work correctly, the sending calculator must be in a power-saving state and it cannot be executing an Assembly program. (The sending calculator is the calculator that's not executing the GetCalc
command.) Although most of the commands are safe to use, the two main commands that you should use to ensure this are Pause
and Menu
.
To use the GetCalc
command, you should first be in the Program editor for your program. In the Program editor, press the PRGM button, then arrow over to the I/O menu. Then, scroll down to GetCalc
and press ENTER. Now the GetCalc
command has been put into your program. You then have to select a variable to use.