Input

Finally, you understand how variables work. Now, let’s use those variables to get input from the user of the program. Using input, you can recognize what keys the user presses, or you might have the user answer a question. Either way, most input is stored in a variable. Figure 2.7 shows the output of this program.

Figure 2.7. The demo02-04.bb program.


;demo02-04.bb asks user's name and shows it
;get the user's name
name$ = Input$("Hi! May I know your name please? ")
Print "Hi " + name$ + "."

;Wait five seconds
Delay 5000

The first line is a comment that tells what the program does. The second line takes in the input, and the third and final line displays what the user entered.

Input$ is declared as this:

Input$(prompt$)

Table 2.3 describes Input$'s parameters.

Table 2.3. Parameters for Input$()
ParameterDescription
prompt$The string displayed to the user before allowing the user to enter an input value.

Caution

Notice that the function name, Input$, has a $ sign attached to the end. This symbol signifies the return type of the function. Because it is a string, the function only returns strings. What this means is that if you request the user to put in numbers to add together, such as 2 + 2, the value returned will be "2 + 2", NOT 4. Of course, if the user typed in 4, the function would return 4.


Input$ is the name of the function. Table 2.3 explains that prompt$ is a string that is displayed to the computer before taking the input value. prompt$ is usually used to ask the user to provide you with the info you want so that the user will know what to tell the program. Notice that there are parentheses around prompt$ in the function. Parentheses are required; if you fail to place them in the program, the program will not compile. Also, notice that there are no brackets around prompt$. This means that the variable is required. If you want to have a blank prompt$, use "" (two quotation marks) as your prompt.

In the previous program, name$ is set equal to the Input$ command. When the Input$ command receives an answer from the user, it is stored in the name$ variable. If you left this line looking like this:

Input$("Hi! May I know your name please?")

without including a variable, the response that the user made would be simply thrown away. Using Input$ without a variable is not a good idea.

Input$ only returns strings (that’s why a $ is added to the function name). However, if the variable you use to retrieve the user input is an integer instead of a string, the value will be interpreted as an integer. Therefore, if you ask the user “How old are you?” and the variable you use to retrieve the value is an integer, the variable will contain whatever the user types in.

Okay, you now have the basics of input down. However, this input function isn’t very useful so far. Who wants a program that tells him his own name? This brings me to the next topic: conditionals.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.15.154