Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.
I've got a menu that prompts user input in the form of a single keypress to do a particular option.
The keypress may be a character or a number and I need to test which is the case and assign the number to a variable if it's a number. Is there a more direct way than the following example?
'Menu is displayed.......
a$ = get_key$() 'sub to loop until keypress and return key$
a = ASC(a$) 'get the ASCII value of the keypress
IF (a >= 49) AND (a <= 57) THEN 'test the character to see if it's 1 to 9
a = val(a$)
IFEND
I've got a funny feeling there's a simpler way to achieve this but I can't think of one.
Thanks everyone
kiiid
Guru
Joined: 11/05/2013 Location: United KingdomPosts: 671
Posted: 06:09pm 01 Aug 2013
Copy link to clipboard
Print this post
See the INSTR function.
You can define a string of possible valid responses for the menu, something like "0123ABCD" and then when you receive the user input in a$ (remove the ascii conversion stuff as it is not needed), execute something like
opt=INSTR(a$,"0123ABCD") and you will receive in opt a number which will indicate the selected choice, or 0 if the choice was invalid.
Edited by kiiid 2013-08-03http://rittle.org
--------------
Blackened
Regular Member
Joined: 11/08/2012 Location: AustraliaPosts: 66
Posted: 06:39pm 01 Aug 2013
Copy link to clipboard
Print this post
ah! I hadn't thought to use INSTR() in that way. I really should go through the library and have a look at the methods other people use.
So I could alter the above snippet to:
'Menu is displayed.......
a$ = get_key$() 'function to loop until keypress and return key$
a = instr(a$,123456789)
Then if "a" returns true in a test I know it represents "a$" as an integer. And if false I test for a character. Cool bananas.
I'd just test a$ against the character "1" or "2" etc.... but I need the value to point to an array element too.
Blackened
Regular Member
Joined: 11/08/2012 Location: AustraliaPosts: 66
Posted: 07:08pm 01 Aug 2013
Copy link to clipboard
Print this post
Just realised I could achieve the exact same results with:
a = val(a$)
I didn't read the manual properly and thought I'd get an error or something if the string wasn't a valid number.
I'm learning....... slowly
shoebuckle Senior Member
Joined: 21/01/2012 Location: AustraliaPosts: 189
Posted: 07:19pm 01 Aug 2013
Copy link to clipboard
Print this post
Well you could say:
Do 'This loops until a key is pressed
A$=inkey$
Loop until A$<>""
A=val(A$)
A$ will always contain the key pressed in character format, whether numeric or non-numeric.
If A now is NOT zero, then one of the keys 1 through 9 has been pressed and it will contain its numeric value.
There is no right or wrong way of coding, provided it works. Yours works fine as long as you change the last line to ENDIF (not IFEND)
Cheers,
Hugh
Just read the other replies... I am too slow!! Back to sleep.Edited by shoebuckle 2013-08-03
Blackened
Regular Member
Joined: 11/08/2012 Location: AustraliaPosts: 66
Posted: 08:15pm 01 Aug 2013
Copy link to clipboard
Print this post
I do the ifend thing all the bloody time . MMEdit saves me from myself
Your inkey$ loop is exactly what's in my get_key() function, except I use LCASE so I only test for lower case characters. I use it so often in my current program that I figured it was worth making it a function and reduce the code elsewhere to a single line. Heaps of menus and sub-menus etc....
domwild Guru
Joined: 16/12/2005 Location: AustraliaPosts: 873
Posted: 02:45pm 11 Aug 2013
Copy link to clipboard
Print this post
More nit picking:
INT is an internal data format, the number (integer) is stored in binary two's complement form after internal conversion from CHAR. Every keyboard input is in CHAR form (ASCII).
The question you are asking is: "Is the character coming in a numeric or something else, e.g., alpha or special character?"Taxation as a means of achieving prosperity is like a man standing inside a bucket trying to lift himself up.
Winston Churchill
robert.rozee Guru
Joined: 31/12/2012 Location: New ZealandPosts: 2350
Posted: 02:56am 12 Aug 2013
Copy link to clipboard
Print this post
the problem with using A$=inkey$ is that the string A$ takes up 256 bytes or so of memory (if i'm not mistaken). a better option would seem to be to set up an ON KEY... interrupt handler, then within the handler do A=asc(inkey$) to extract the character code.
looking at val(), it seems to suffer from the problem of returning 0 for both "" and "0", hence can not distinguish between the zero key and a non-numeric. strictly speaking, val should return a 'not a number' value for any string it can not evaluate.
this could be an enhancement for mmbasic, define a set of constants for attempting to evaluate invalid arguments. this would apply to val(), asc(), and i am sure there are some others. for backwards compatibility, NaN could be arranged to evaluate to a boolean FALSE.
perhaps something for version 4.5 :-)
MicroBlocks
Guru
Joined: 12/05/2012 Location: ThailandPosts: 2209
Posted: 05:41am 12 Aug 2013
Copy link to clipboard
Print this post
With the latest version you can define the length of strings in an array
DIM A$(0) LENGTH 1
Uses only one byte (plus a little overhead of course)
A$(0) = inkey$
Another way, maybe even easier, would be to get the character code as quickly as possible and get it into the right range.
A = ASC(inkey$) - 48
You can then check if A is between 0 and 9
IF A>=0 AND A <=9 THEN ....
Comparing numbers is quicker and uses less memory.