Using Liberty Basic

Want to learn to program using Liberty Basic? Already programming in Liberty Basic but want to learn more? Follow this experienced programmer (Java) as he teaches himself this wonderful programming language.

 

Thursday, April 27, 2006

Getting User Input

If you have been following me so far, we now know how to store data in our programs as well as how to output that data to the screen. The next logical step for us is to be able to receive data from the user, and know how to store that data as variables in our programs.

As this blog progresses, we'll move on to creating windows programs that use the various windows gadgets you are all used to seeing as part of a windows program. I'm talking about things like textboxes, buttons, menus, lisboxes etc. When we discuss these components we'll also discuss how to get user input through them. For right now, however, we are dealing with the 'main window' that gets displayed by Liberty Basic as the default user interface for our Liberty Basic programs.

So, how can we get any input from the user via this 'main window'? Here is an example program...


' =============================
' Getting User Input
' =============================
' Program to demonstrate receiving some user input.
' Author: Eddie Meyer
' Date: 27th April 2006

print "This program will ask you for some information."
print "After each prompt, please enter the requested information"
print "and then hit 'Enter'."
print
input "Please enter your full name: "; FullName$
input "Please enter your age: "; Age
print
print "Your name is "; FullName$; " and you are "; Age; " years old."

end


When you run this program the following is printed...


This program will ask you for some information.
After each prompt, please enter the requested information
and then hit 'Enter'.

Please enter your full name:


The program then waits until you enter your name and hit the 'Enter' (Return) key. Actually, technically, the program only waits until you hit the 'Enter' key. Basically, anything you type before hitting the 'Enter' key is stored in the variable FullName$. If you didn't enter anything and just hit the enter key, nothing (the empty string ""), will be stored in the FullName$ variable. Anyway, after hitting the 'Enter' key, the program then continues running. It then displays the following (see below) and again waits for user input.


Please enter your age:


This time, anything entered before the 'Enter' key is hit will be placed in the Age variable. Note: In this case the variable in question is expected to have a value that is a number. Do you remember why? It is because the variable Age was defined without a trailing dollar sign ($). If it had been defined with a trailing dollar sign ($), then we (and the Liberty Basic compiler) would know to expect a string. You may ask why I am pointing this out. I'm mentioning this because users don't always do what we expect them to. For example, a 30 year old user using our program might type in "thirty" instead of the numerical 30 that we are expecting. If they did do this, the Age variable would be set to contain the default value of 0 (because the user didn't enter a number at all... or at least, they didn't enter a number in numerical form). Interestingly, if the user typed in a string that started with a number, that number at the beginning of the string would be used. For example, if the user entered an age of "9.5inches", the value 9.5 would be stored in the Age variable. It is good practice to always check the validity of the input into your program. In our case, we probably would want to check that the user did actually enter a name and that they also entered a valid number as their age. This issue of input validation is a subject in it's own right. I'm sure I will tackle it in a later post. Leave me a comment if this is something you would be interested in me covering.

Anyway, I think that's it for now. I hope you enjoyed this post.

Thanks

Eddie

P.S. One other point to note about my program in this post. There are a couple of lines that only contain the 'print' command and nothing else. These are used just to print out a new line for spacing purposes. If you look at the example output I provided above, you should be able to see what I mean (e.g. there is a blank line between the header information and the first question the program asks).

0 Comments:

Post a Comment

<< Home