How can i limit the number of characters in line input statement?

I'm making a game that asks your name at the start. The question is in a cool looking box and i want that the maximum number of characters is limited to 10 so you cant write "out of the box". How can i do that? I'm using a statement
"LINE INPUT "Your name"; name$" at the moment. Please help me!

Comments

  • : I'm making a game that asks your name at the start. The question is in a cool looking box and i want that the maximum number of characters is limited to 10 so you cant write "out of the box". How can i do that? I'm using a statement
    : "LINE INPUT "Your name"; name$" at the moment. Please help me!

    The trick is not to use INPUT but INKEY$

    DO
    a$=INKEY$
    name$=name$+a$
    LOOP UNTIL a$ = CHR$(the ascii code for -enter- here) OR LEN(name$) = 10

    Not sure it will work did'nt have time to test it.

  • : I'm making a game that asks your name at the start. The question is in a cool looking box and i want that the maximum number of characters is limited to 10 so you cant write "out of the box". How can i do that? I'm using a statement
    : "LINE INPUT "Your name"; name$" at the moment. Please help me!
    :

    This code is a bit long, but it does the job well. Hope you understand all my internal comments!

    InputText$ = "" ' Initial text to be in the input field
    MaxLength% = 10 ' This sets the maximum number of characters the
    ' user can enter, change it to whatever you want.
    COLOR 0, 7 ' Change colours to whatever you want
    DO
    LOCATE 10, 10: PRINT InputText$; STRING$(MaxLength% - LEN(InputText$), " ")
    LOCATE 10, 10 + LEN(InputText$) - ((LEN(InputText$) < MaxLength%) + 1), 1
    DO
    KeyPress$ = INKEY$
    LOOP WHILE KeyPress$ = ""
    SELECT CASE KeyPress$
    CASE CHR$(8)
    IF InputText$ <> "" THEN
    InputText$ = LEFT$(InputText$, LEN(InputText$) - 1)
    END IF
    CASE CHR$(13)
    EXIT DO
    CASE CHR$(27)
    END ' Change this & put the code here to do whatever
    ' happens if user presses the escape key
    CASE ELSE
    IF KeyPress$ >= " " AND KeyPress$ <= "" THEN
    InputText$ = LEFT$(InputText$ + KeyPress$, MaxLength%)
    END IF
    END SELECT
    LOOP
    ' Program continues here once Enter is pressed - the text the
    ' user has entered is saved in the variable InputText$.

  • : I'm making a game that asks your name at the start. The question is in a cool looking box and i want that the maximum number of characters is limited to 10 so you cant write "out of the box". How can i do that? I'm using a statement
    : "LINE INPUT "Your name"; name$" at the moment. Please help me!
    :

    This code is a bit long, but it does the job well. Hope you understand all my internal comments!

    InputText$ = "" ' Initial text to be in the input field
    MaxLength% = 10 ' This sets the maximum number of characters the
    ' user can enter, change it to whatever you want.
    COLOR 0, 7 ' Change colours to whatever you want
    DO
    LOCATE 10, 10: PRINT InputText$; STRING$(MaxLength% - LEN(InputText$), " ")
    LOCATE 10, 10 + LEN(InputText$) - ((LEN(InputText$) < MaxLength%) + 1), 1
    DO
    KeyPress$ = INKEY$
    LOOP WHILE KeyPress$ = ""
    SELECT CASE KeyPress$
    CASE CHR$(8)
    IF InputText$ <> "" THEN
    InputText$ = LEFT$(InputText$, LEN(InputText$) - 1)
    END IF
    CASE CHR$(13)
    EXIT DO
    CASE CHR$(27)
    END ' Change this & put the code here to do whatever
    ' happens if user presses the escape key
    CASE ELSE
    IF KeyPress$ >= " " AND KeyPress$ <= "" THEN
    InputText$ = LEFT$(InputText$ + KeyPress$, MaxLength%)
    END IF
    END SELECT
    LOOP
    ' Program continues here once Enter is pressed - the text the
    ' user has entered is saved in the variable InputText$.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories