Code fix - Printable Version +- QB64 Phoenix Edition (https://staging.qb64phoenix.com) +-- Forum: Chatting and Socializing (https://staging.qb64phoenix.com/forumdisplay.php?fid=11) +--- Forum: General Discussion (https://staging.qb64phoenix.com/forumdisplay.php?fid=2) +--- Thread: Code fix (/showthread.php?tid=1058) Pages:
1
2
|
Code fix - Chris - 11-06-2022 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# RE: Code fix - OldMoses - 11-06-2022 Where are 15 & 20? The IDE is looking for line labels to go to, otherwise it throws an error. RE: Code fix - mnrvovrfc - 11-06-2022 (11-06-2022, 11:51 AM)Chris Wrote: HelloWelcome 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). RE: Code fix - OldMoses - 11-06-2022 If I'm understanding the problem correctly, this should achieve the desired effect. Code: (Select All) PRINT "Enter a digit" and if you need the escape and spacebar options... Code: (Select All) PRINT "Enter a digit" RE: Code fix - SMcNeill - 11-06-2022 PRINT "Enter a digit" DO K$ = INPUT$(1) LOOP UNTIL K$ >= "0" AND K$ <= "6" PRINT K$ RE: Code fix - Chris - 11-06-2022 Thank you very much. That's what I meant, it works as it should. RE: Code fix - OldMoses - 11-06-2022 (11-06-2022, 12:59 PM)SMcNeill Wrote: PRINT "Enter a digit" Ooooh, INPUT$, I never noticed that one before. A pretty new bauble to play with. RE: Code fix - SMcNeill - 11-06-2022 (11-06-2022, 01:11 PM)OldMoses Wrote:(11-06-2022, 12:59 PM)SMcNeill Wrote: PRINT "Enter a digit" 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. RE: Code fix - SMcNeill - 11-06-2022 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.) =>"; RE: Code fix - Dimster - 11-06-2022 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. |