Classic Computer Magazine Archive COMPUTE! ISSUE 76 / SEPTEMBER 1986 / PAGE 10

Reader's Feedback

The Editors and Readers of COMPUTE!

If you have any questions, comments, or suggestions you would like to see addressed in this column, write to "Readers' Feedback," COMPUTE!, P.O. Box 5406, Greensboro, NC 27403. Due to the volume of mail we receive, we regret that we cannot provide personal answers to technical questions.


STRING$, SPACE$, And CHR$

I have a suggestion for people who submit or translate IBM PC/PCjr programs for publication in your magazine. Whenever a BASIC program line requires that I type a long series of spaces, I find it difficult to tell exactly how many spaces are needed. This can be frustrating, because the "Automatic Proofreader' keeps signaling an error until I finally get the right number by trial and error. The STRING$ function can easily eliminate this problem. For instance, the statement PRINT STRING$(15,32) has exactly the same effect as PRINT "               " and is much easier to type in. STRING$ can be used where any long series of identical characters is needed. For instance, PRINT STRING$(40,46) prints a line consisting of 40 dots.

Richard J. Patton

This is an excellent suggestion, and the same general advice applies to every version of BASIC. Some versions include STRING$, which works exactly as in IBM BASIC; Amiga BASIC even includes a specialized SPACE$ function for creating a string of spaces. For BASICs that don't support either function, you can do the same job through concatenation. To create a string consisting of 30 spaces, for instance, use SP$="":FOR J=1 TO 30:SP$=SP$+CHR$(32):NEXT. This construction is easy to type and requires only a few more characters than printing the string in literal form.

For similar reasons, it's often preferable to express graphic characters or unusual symbols as CHR$ values rather than as string literals. Here are two different versions of a typical Commodore BASIC line:

10 IF X$=" " THEN GOSUB 100
10 IF X$=CHR$(135) THEN GOSUB 100

The first version of line 10 uses a literal graphics character to test whether the F1 function key has been pressed. The second version performs the same test with CHR$. To alleviate the "mysterious character" problem, our listing conventions (see "COMPUTE!'S Guide to Typing In Programs" elsewhere in this issue) replace any unusual Commodore or Atari character with a sequence that's easier to read. Here's what the same line would look like in a COMPUTE! listing:

10 IF X$="{F1}" THEN GOSUB 100

That's an improvement over listing an indecipherable graphics symbol, but it still requires that you remember the listing convention or look it up when the time comes. Of these three alternatives, the line with CHR$ is preferred in many cases, since it's easy to read and type, and doesn't require reference to anything but the listing. Of course, where large numbers of characters are involved, CHR$ may not be practical.