Input

[ LiB ]

Finally, we 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 him 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 name$ = Input$("Hi! May I know your name please? ") Print "Hi " + name$ + "." WaitKey 

NOTE

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 num bers 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.

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. Input$()'s Parameter

Parameter

Description

prompt$

The string displayed to the user before allowing the user to enter an input value.


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, it 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, we now have the basics of input down. However, this input function isn't very useful so far. Who wants a program that tells them their own name? This brings us to our next section: conditionals.

[ LiB ]


Game Programming for Teens
Game Programming for Teens
ISBN: 1598635182
EAN: 2147483647
Year: 2004
Pages: 94
Authors: Maneesh Sethi

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net