<< Previous TopicReturn to Index  

How do I limit user input

June 14 2017 at 7:07 PM
Leo  (Login leostout)

 
So as you are probably aware, when coding in Qbasic (or any basic for that matter) if you don't have a way to stop user input after so many characters, then some clown down the road is going to continue typing nonsense into your input line, just to watch it scroll to the next line and so on, until your page disappears and the program crashes.

So, I am trying to figure out how to stop user input after they type, say 10 characters (for example). I know there's a way to do this. I believe it has something to do with the LEN statement, possibly RIGHT$, but I cannot seem to figure it out. If someone would be so kind as to give me the code, I would be forever grateful and offer sacrifices to whatever god you worship with one of my fatted calves. Thanks!

 
 Respond to this message   
AuthorReply
Kewbie
(Login Kewbie)
R

Here's hoping this isn't someone's homework...

June 17 2017, 9:18 AM 

OK, this is one possible method.

Fault #1: Please note that, while BS is handled, it is not handled properly, and will mess up your screen.

Fault #2: There is no cursor.

I leave it as an exercise for you, Leo, to add the code to correct these (and any other) faults.

Edit: I decided to allow optional blank entry suppression.

Kew

maxChars = 12
PRINT "enter up to"; maxChars; " characters"
PRINT

myString$ = gimmeChars$(maxChars, -1) 'ANY non-zero value will allow blank entries

PRINT: PRINT
PRINT "You entered '"; myString$; "'"

PRINT: PRINT: PRINT
PRINT "please enter your name (25 characters max)"

myName$ = gimmeChars$(25, 0) 'Zero disallows blank entries

PRINT: PRINT
PRINT "You entered '"; myName$; "'"
END

FUNCTION gimmeChars$ (howMany AS INTEGER, allowBlankEntries AS INTEGER)
ret$ = ""

retry:
DO
'get a char from keyboard
ch$ = ""
WHILE ch$ = ""
ch$ = INKEY$
WEND
IF ASC(ch$) > 31 THEN
IF LEN(ret$) < howMany THEN
PRINT ch$;
ret$ = ret$ + ch$
ELSE
BEEP
END IF
ELSE
'leave if enter key hit
IF ch$ = CHR$(13) THEN EXIT DO

'remove last character if BS hit
IF ch$ = CHR$(8) THEN
ret$ = LEFT$(ret$, LEN(ret$) - 1)
PRINT
PRINT ret$;
END IF

'return empty string if Esc hit
IF ch$ = CHR$(27) THEN
ret$ = ""
EXIT DO
END IF
END IF
LOOP

'error handling is the ONE place where I will use GOTOs
'SO KILL ME!
IF ret$ = "" AND allowBlankEntries = 0 THEN
BEEP
PRINT 'Leo, fix this when you fix BS
GOTO retry
END IF

gimmeChars$ = ret$
END FUNCTION


    
This message has been edited by Kewbie on Jun 17, 2017 9:55 AM


 
 Respond to this message   

Pete
(Login The-Universe)
Admin

Here's one with a clock in the title bar.

June 17 2017, 9:55 PM 

The clock will keep running while the input is being typed. It is a custom keyboard input routine, which is a way around the limitations of the INPUT statement. It is limited to 10-character input by the variable: maxspaces.

Have fun trying it out.

- Pete

---------------------------------------------------

maxspaces% = 10
WIDTH 80, 25
DO
    CLS
    LOCATE 10, 5
    PRINT "Input Name: ";
    showcursor% = 7 ' Use 7 to show the cursor or zero to hide it. A value of 30 makes it a small square.

    CALL getinput(word$, maxspaces%, limitinput$, showcursor%, autoenter%)

    LOCATE 12, 5: PRINT "The input is now in the variable word$ as: "
    LOCATE 14, 41 - LEN(word$) / 2 ' Centers the print in the next line.
    PRINT word$

    ' Now here is another way to use the sub and keep the clock running.
    LOCATE 16, 5
    PRINT "Try again? Y/N "; ' Remember to always use a ";" after the print statement to keep your input on the same line.
    limitinput$ = "YyNn"
    autoenter% = 1 ' Automatically enter response after one key is pressed.
    showcursor% = 0 ' Hide the cursor for yes/no responses.

    CALL getinput(word$, maxspaces%, limitinput$, showcursor%, autoenter%)

    limitinput$ = "" ' Always erase variable for other uses.
    autoenter% = 0 ' Always erase variable for other uses.
    IF LCASE$(word$) = "n" THEN END
LOOP

SUB getinput (word$, maxspaces%, limitinput$, showcursor%, autoenter%)
' Input Statement with clock.
startpos% = POS(0)
endpos% = startpos% + maxspaces%
word$ = "" ' Erases last entry to begin next entry.
LOCATE , , 1, 7, showcursor%

DO
    IF TIME$ >= "12:00:00" THEN
        TIME1 = ASC(MID$(TIME$, 1, 1)) - 1
        TIME2 = ASC(MID$(TIME$, 2, 1)) - 2
        IF TIME2 < 48 THEN TIME2 = TIME2 + 10: TIME1 = TIME1 - 1
        TIMECONV$ = CHR$(TIME1) + CHR$(TIME2) + MID$(TIME$, 3, LEN(TIME$)) + " PM"
    ELSE
        TIMECONV$ = MID$(TIME$, 1, 8) + " AM"
    END IF
    IF MID$(TIMECONV$, 1, 2) = "00" THEN MID$(TIMECONV$, 1, 2) = "12"
    _TITLE SPACE$(61) + TIMECONV$

    ' KEY INPUT GOES HERE....................................................

    IF autoenter% THEN
        ' Used for a non-enter response, like answering yes or no.
        IF LEN(word$) = autoenter% THEN EXIT SUB
    END IF

    mykey$ = INKEY$

    IF mykey$ <> "" THEN
        yy% = CSRLIN: xx% = POS(0)

        SELECT CASE mykey$
            CASE CHR$(0) + "K"
                mov% = -1: GOSUB action
            CASE CHR$(0) + "M"
                mov% = 1: GOSUB action
            CASE CHR$(0) + "S"
                GOSUB wash: GOSUB delete
            CASE CHR$(0) + "R"
                IF ins% = 0 THEN ins% = -1 ELSE ins% = 0
                IF ins% = 0 THEN LOCATE , , 1, 7, 7 ELSE LOCATE , , 1, 7, 30
            CASE CHR$(0) + "O"
                IF word$ <> "" THEN LOCATE yy%, startpos% + LEN(word$)
            CASE CHR$(0) + "G"
                LOCATE yy%, startpos%
            CASE CHR$(8)
                IF xx% > startpos% THEN
                    LOCATE , POS(0) - 1
                    xx% = POS(0)
                    GOSUB wash: GOSUB delete
                END IF
            CASE CHR$(13)
                ' Exits the input sub back to your main program.
                EXIT SUB
            CASE CHR$(27)
                ' Quit and removes program winsow.
                SYSTEM
            CASE CHR$(32) TO CHR$(126)
                IF INSTR(limitinput$, mykey$) OR limitinput$ = "" THEN
                    key$ = mykey$
                    GOSUB action
                ELSE
                    BEEP ' Input error, retry.
                END IF
        END SELECT
        mov% = 0: key$ = ""
    END IF
LOOP

action:
IF POS(0) + mov% >= startpos% AND POS(0) + mov% < endpos% THEN
    DO
        IF key$ <> "" THEN
            SELECT CASE ins%
                CASE 0
                    IF LEN(word$) + LEN(key$) > endpos% - startpos% THEN EXIT DO
                    word$ = MID$(word$, 1, POS(0) - startpos%) + key$ + MID$(word$, POS(0) - startpos% + 1)
                CASE -1
                    word$ = MID$(word$, 1, POS(0) - startpos%) + key$ + MID$(word$, POS(0) - startpos% + 2)
            END SELECT
        END IF
        IF POS(0) - startpos% >= LEN(word$) - LEN(key$) AND key$ <> "" OR key$ = "" OR ins% = -1 THEN
            IF key$ = "" AND mov% = 1 THEN IF POS(0) - startpos% = LEN(word$) THEN EXIT DO
            LOCATE , POS(0) + mov%: PRINT key$;
        ELSE
            LOCATE yy%, startpos%: PRINT MID$(word$, 1, xx% - startpos%); key$;: xx2% = POS(0): PRINT MID$(word$, xx% - startpos% + 2);: LOCATE yy%, xx2%
        END IF
        EXIT DO
    LOOP
END IF
RETURN

wash:
IF POS(0) >= startpos% AND word$ <> "" AND POS(0) - startpos% < LEN(word$) THEN
    LOCATE yy%, startpos% + LEN(word$) - 1
    PRINT " ";
    LOCATE yy%, xx%
END IF
RETURN

delete:
IF POS(0) - startpos% = 0 THEN
    word$ = MID$(word$, 2)
ELSE
    word$ = MID$(word$, 1, POS(0) - startpos%) + MID$(word$, POS(0) - startpos% + 2)
END IF
PRINT MID$(word$, xx% - startpos% + 1);: LOCATE yy%, xx%
RETURN
END SUB

 
 Respond to this message   
 
  << Previous TopicReturn to Index  
 Copyright © 1999-2017 Network54. All rights reserved.   Terms of Use   Privacy Statement