BASIC Instincts

SCREEN I/O TECHNIQUES: PART 1

by Gene Barker

Reprinted from Commodore World Issue #7



In Commodore BASIC v2 we have the handy INPUT command. If we wanted to grab a string from the user (such as the user's name), we would use a line such as:
     100 INPUT"NAME",N$
Or if we wanted to grab a number from the user (such as the user's age), we would use a line such as:
     110 INPUT"AGE",A
When RUN, the INPUT statement will ask the user for the desired information and place the data into the variables N$ and A. But there's one problem. What happens if the user accidentally presses a cursor down key or types a letter instead of a number? You may get a ?REDO FROM START or perhaps an ?EXTRA IGNORED error. What if you want the ability to stop the program by using the [RUN/STOP] key? The INPUT statement ignores it. Maybe there's a better way.

In this issue of BASIC Instincts we'll give you three subroutines which will help you input text and numbers in a more graceful and professional manner. Now your BASIC programs can be just as friendly as those high tech Machine Language ones. Onward...

Displaying The Cursor
We need a way to tell the user that our program expects input of some kind. BASIC's flashing cursor is a great way to accomplish this. To turn the cursor on and off, all we must do is:

     POKE 204,0            (to turn the cursor ON)
     POKE 204,1            (to turn the cursor OFF)
You will find that the first two subroutines, INPUT STRING (SUB) and INPUT INT STRING (SUB) use this method. Don't let the POKE statements confuse you. BASIC knows where the cursor is at all times. When we change the value at memory location 204 with the POKE statement, we are simply telling BASIC whether or not to display the cursor.

Intercepting Keys
All three of the subroutines in this column require the ability to intercept keys as they are typed. To accomplish this, we intercept keys one at a time with a statement like the following:

     100 GET XX$:IF XX="" THEN 100
Each time the user presses a key, it is placed in the character buffer (the buffer can hold up to ten characters). We use the GET statement to pull these characters out of the buffer one at a time. In the above statement, we grab the character that is first in line with GET, and place it in the string variable XX$. If there are no characters waiting in the buffer then the variable XX$ will equal "". The double quotes indicate a null character (no character available). The IF statement makes sure that we have a valid character before moving on to the next line.

Formatted Integers
Have you ever noticed that BASIC prints a leading space in front of positive integers? BASIC always prints positive integers in this manner and aligns the integers to the left. Our third subroutine INPUT RANGED INT (SUB) allows the user to use the plus and minus keys to select the integer they wish to enter. To keep the output clean and professional, we use BASIC's string formatting statements STR$, MID$, and RIGHT$. Let's take a closer look. Before we can manipulate a number, we must first convert it to a string. BASIC's STR$ function makes this simple. Example:

     A          STR$(A)
     5          " 5"
     -100       "-100"
     654321     " 654321"
Notice how BASIC adds a leading space when converting positive integers to a string. To remove this leading space we can use the MID$ function. Example:
     A          MID(STR$(A),2)
     5          "5"
     -100       "100"
     654321     "654321"
The MID$ function removes the leading space from any positive integer (A). Notice how the function strips the negative sign from -100. Now, how can we left align our integer (A) to five digits and pad the leading digits with zeros? This can be done through the strategic use of the RIGHT$ function. Example:
     A          RIGHT$("0000"+MID(STR$(A),2),5)
     5          "00005"
     -100       "00100"
     654321     "54321"
Note how we lost the leading digit of 654321. When we use the RIGHT$ function for padding, we should always be careful to provide for enough digits.

See if you can follow how the INPUT RANGED INT (SUB) subroutine uses this method.

The Subroutines
The type in program includes the three following subroutines:

INPUT STRING (SUB) - Inputs a string from the user
INPUT INT STRING (SUB) - Inputs a positive integer string from the user
INPUT RANGED INT (SUB) - Inputs a positive integer from the user

At the top of the program is a sample call for each subroutine. The subroutines are written for readability, not speed. You will find that they are well documented and easy to read. You are free to use these subroutines in your own programs (commercial included).

Notes
As you type in this issue's program, take it one section at a time. Try to get a general idea of what the section is trying to accomplish. See if you can follow how the section is using its variables. If you see an unfamiliar BASIC statement, take a quick look at it in your BASIC manual. If you are still confused, move on to the next section; often times the next section helps explain the previous one. Above all, don't forget to BACKUP your work frequently.
 
 


View Sample Program


[Sample Issue] [CMDRKEY.com Home]


Copyright © 2002 Click Here Software Co.
Comments and questions regarding this site
should be directed to support@cmdrkey.com