Code fix
#1
Hello
Please modify the code.
Normal inputs are: zero (inclusive) to 6
How to convert the code so that the option without entering digits after pressing the Enter key, T # takes the value 7.
Thank You

PRINT "Enter a digit"
K$ = "": F$ = ""
WHILE LEN(K$) < 1 AND F$ <> CHR$(13)
    F$ = ""
    WHILE F$ = "": F$ = INKEY$: _LIMIT 30: WEND
    IF F$ = CHR$(27) THEN 15
    IF F$ = CHR$(32) THEN 20
    IF F$ <> CHR$(13) THEN K$ = K$ + F$
WEND
T# = VAL(K$): PRINT T#
Reply
#2
Where are 15 & 20? The IDE is looking for line labels to go to, otherwise it throws an error.
DO: LOOP: DO: LOOP
sha_na_na_na_na_na_na_na_na_na:
Reply
#3
(11-06-2022, 11:51 AM)Chris Wrote: Hello
Please modify the code.
Welcome to the forums. Next time please try to use a better topic name than "Code fix" because it's too curt.

Also your style of programming could use some improvement. Line numbers as well as "WHILE... WEND"? Why?

You need to use "ASC()" function to check the ASCII codes of the result of "INKEY$" if you only desire numerals, which are from 48 (zero) to 57 ("9"). So far you're only checking special keys which aren't alphanumerics.

Using "VAL()" is a bit complicated because if the user types zero first, that function doesn't tell the difference if that's what it is, or if it's not a numeral. Therefore might want to set the start of the range to "1" which is CHR$(49).
Reply
#4
If I'm understanding the problem correctly, this should achieve the desired effect.

Code: (Select All)
PRINT "Enter a digit"
K$ = "": F$ = ""
DO
    F$ = INKEY$
    IF F$ <> "" THEN
        IF F$ = CHR$(13) THEN
            F$ = "7"
        ELSE
            IF INSTR("0123456", F$) THEN F$ = MID$("0123456", INSTR("0123456", F$), 1) ELSE F$ = ""
        END IF
        K$ = K$ + F$
    END IF
    _LIMIT 30
LOOP UNTIL LEN(F$) <> 0
T# = VAL(K$): PRINT T#


and if you need the escape and spacebar options...

Code: (Select All)
PRINT "Enter a digit"
K$ = "": F$ = ""
DO
    F$ = INKEY$
    IF F$ <> "" THEN
        IF F$ = CHR$(13) THEN
            F$ = "7"
        ELSEIF F$ = CHR$(27) THEN
            GOTO escape
        ELSEIF F$ = CHR$(32) THEN
            GOTO spacebar
        ELSE
            IF INSTR("0123456", F$) THEN F$ = MID$("0123456", INSTR("0123456", F$), 1) ELSE F$ = ""
        END IF
        K$ = K$ + F$
    END IF
    _LIMIT 30
LOOP UNTIL LEN(F$) <> 0
T# = VAL(K$): PRINT T#
END

escape:
'do whatever escape is supposed to do
spacebar:
'do whatever the spacebar is supposed to do
DO: LOOP: DO: LOOP
sha_na_na_na_na_na_na_na_na_na:
Reply
#5
PRINT "Enter a digit"
DO
K$ = INPUT$(1)
LOOP UNTIL K$ >= "0" AND K$ <= "6"
PRINT K$
Reply
#6
Thank you very much. That's what I meant, it works as it should.
Reply
#7
(11-06-2022, 12:59 PM)SMcNeill Wrote: PRINT "Enter a digit"
DO
    K$ = INPUT$(1)
LOOP UNTIL K$ >= "0" AND K$ <= "6"
PRINT K$

Ooooh, INPUT$, I never noticed that one before. A pretty new bauble to play with. Wink
DO: LOOP: DO: LOOP
sha_na_na_na_na_na_na_na_na_na:
Reply
#8
(11-06-2022, 01:11 PM)OldMoses Wrote:
(11-06-2022, 12:59 PM)SMcNeill Wrote: PRINT "Enter a digit"
DO
    K$ = INPUT$(1)
LOOP UNTIL K$ >= "0" AND K$ <= "6"
PRINT K$

Ooooh, INPUT$, I never noticed that one before. A pretty new bauble to play with. Wink

INPUT$ is as old as the language itself -- nothing new or pretty about it!  Honestly though, INPUT$ should get a *LOT* more use than what it does.  What it does is:

1) Wait for a set number of keypresses.  (No need for a sleep, limit, or _delay in any loop -- it does all that itself.)
2) Return that number of keypresses back to you.

INPUT$(1) wants 1 keypress.   INPUT$(3) waits for the user to press 3 keys.  (Or one key 3 times.)

It's simple.  It's very efficient.  Why it's not used more is beyond me.  Wink
Reply
#9
An example which uses INPUT$(1) to build a string of digits from 1 -6, as a sort of passcode, which seems a bit more like the original post:

Code: (Select All)
Print "Enter the passcode.  (All digits are from 1 to 6.) =>";
Do
    Do
        K$ = Input$(1)
    Loop Until (K$ >= "0" And K$ <= "6") Or K$ = Chr$(13)
    Print K$;
    response$ = response$ + K$
Loop Until K$ = Chr$(13)
Print
Print response$
Reply
#10
I'm with Old Moses - that Input is one I'm not familiar with. Has it been around a long time? Is there a reason why it only deals with string values (ie Input(4) would/might be useful for non string values). I have a lot of data stored in numeric values, does Input$ automatically convert the values to strings or do they need to be stored as string values to work with Input$. It's amazing how rich QB64pe language is getting.
Reply




Users browsing this thread: 4 Guest(s)