[futurebasic] [FB3] DIM 1 string$

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : June 2000 : Group Archive : Group : All Groups

From: scram@...
Date: Sat, 3 Jun 2000 10:01:25 -0600
Michael S Kluskens wrote:

>I'm trying to figure out how to properly dimension strings
>Here's my simple example:
>
>LOCAL FN main
>DIM 1 name$
>PRINT "Hi! What's your first name?"
>INPUT name$
>PRINT "Goodbye, ";name$
>END FN
-----<< Snip >>------
>I'd assume the "name$" can hold only a single character but it turns
>out that the compiler lets you enter many characters into the string
>using the input statement (I didn't test other means. I need input to
>work safely for now).  The real bad thing is that if you enter enough
>characters you get what they call in network security "a buffer
>overflow"
=============================

Hi Michael;

This isn't a problem with dimensioning strings but with the old-fashioned
INPUT instruction. I used to have the same problem when using INPUT. Edit
fields are really much better for entering data and with ON EDIT you can
controll exactly what is entered.

However, that may be a too much at first in a big conversion project from C
like you described. Since INPUT can accept more characters than the length
of the destination string$, the solution is to create your own input
function to use instead. Here's one that I put together similar to what I
did in the old days (before FB & PG). Hope this helps. PS it accepts
leading & trailing spaces too, unlike INPUT.  I've copied it below for you.

Add FN InputStr$ shown below to your program.

Then use ...
    theName$ = FN InputStr$("Hi! What's your first name? ", 1 )
in place of ...
    PRINT "Hi! What's your first name?"
    INPUT name$
or...
    theName$ = FN InputStr$("", 1 )
in place of ...
    INPUT name$

Miscellaneous:

  1.  INPUT in FB does not display an extra space after the prompt message.
You should include one so the entered data is separated slightly from the
prompt.

  2.  'name$' is a reserved word in some versions (FBII, not FB^3).

  3.  My routine erases a character by reprinting it in white. That won't
work on non-white backgrounds so the function may need modification there.
Ditto for the printing colour, mine assumes black. There are functions to
get the foreground and background colours but I didn't include them.

  4.  I noticed also in System 9 with anti-aliasing turned for text, the
erasing isn't perfect -- the character is erased but not the extra few grey
pixels around it. Perhaps the routine should fill the entire frame of the
erased character with the background colour. I'll leave that up to you.
(It's little details like that that make me appreciate Edit Fields.)

Good luck with the conversion. Remember, if there isn't a BASIC command to
do what you need, create your own function for it. I used to have a similar
one to enter only digits too.

- Hope this helps.
  Stu Cram, Regina, SK CANADA
==============================

LOCAL FN InputStr$( prompt$, N )
'
' Displays the given prompt$
' Allows only N chacters to be entered from keyboard
' Stops when the 'return' or 'enter' key is pressed
' Delete key will erase the last shown character
' Other control characters (ASCII < 32) are ignored.
'       Sample use:
'       A$ = FN InputStr$( "Enter 5-letter code: ", 5 )
'       B$ = FN InputStr$( "Y or N? ", 1 )
'       C$ = FN InputStr$( "", nam$ )
'
DIM result$ 	' for entire input string
DIM 1 key$	' for currently typed character
DIM done	' flag (0=continue; 1=finished)
     		'     ( set by return or enter)
DIM L		' for length of current input string
DIM h,v		' for position of start of input
DIM sw		'    (used to erase characters)

done = 0
result$ = ""
print prompt$;
h = WINDOW( _penH )
v = WINDOW( _penV )

DO
  L = LEN( result$ )
  key$ = ""
  DO                            ' loop to read a single character
    key$ = INKEY$
  UNTIL LEN( key$ ) = 1

  SELECT key$

    CASE CHR$( 13 ), CHR$( 3 )  'return or enter key
      key$ = ""
      done = 1'  (set flag to quit)

    CASE CHR$( 8 )              'delete key
      LONG IF L > 0             '  (erase last character, if there is one)
        COLOR _zWhite
        sw = FN stringwidth( LEFT$( result$, L-1 ) )
        PRINT %( h + sw, v ) RIGHT$( result$, 1 ) ;
        COLOR _zBlack
        PRINT %( h, v ) ;
        result$ = LEFT$( result$, L-1 )
        key$ = ""
      END IF

    CASE < CHR$( 32 )             'other control characters
      key$ = ""                   ' (convert to null char)

    CASE else                     'regular charcters
       LONG IF L < N              ' (if result has less than N characters
         result$ = result$ + key$ '  add the character to end of result.)
       XELSE
         key$ = ""                ' (otherwize, convert it to a null char)
       END IF

    END SELECT

    PRINT key$;                   ' display the new charcter

  UNTIL done = 1
  PRINT                           ' start a new line
END FN = result$
'---------------------------------

' Main Program to test above function
DIM A$, B$

WINDOW 1, " FN inputStr$() Demo ", ( 100, 200 )-( 500, 400 ), 5
TEXT _Geneva, 14, 1

PRINT
A$ = FN inputStr$( "What's the 5-letter code? ",  5 )
PRINT A$

PRINT
B$ = FN inputStr$( "Pick a letter from A to Z: ", 1 )
PRINT B$

STOP
=============================