Page 1 of 1

Why "C"?

Posted: Wed May 26, 2021 5:53 pm
by LoadError
I have this (t2p formatted) code:

@input:
IF INKEY$="3" THEN goto @theend
IF INKEY$ <>"1" AND INKEY$ <>"2" THEN GOTO @input
LET D=VAL(INKEY$)
[...]
@theend
[...]

("3" jumps to the end of the program, "1" or "2" roll one or two dice respectively).

Rarely (let's say 1% of the time), when this part of the code is parsed, the program stops and I get a "C" report code on the boldface line, which according to the manual means "The text of the (string) argument of VAL does not form a valid numerical expression".
This is surprising, considering that when this happens I'd have just typed either 1 or 2, and even if I had typed any other key, the previous line would have caused the offending line not to be processed.
What do you think is going on?

Re: Why "C"?

Posted: Wed May 26, 2021 7:18 pm
by olofsen
Perhaps it is best to call INKEY$ once

Code: Select all

LET I$ = INKEY$
and test only I$ in the subsequent code.

Re: Why "C"?

Posted: Thu May 27, 2021 10:41 am
by siggi
First check the meaning of the error message C!

If no key is pressed, INKEY$ gives an empty string "", which is NOT a numerical value.

HTH Siggi

Re: Why "C"?

Posted: Thu May 27, 2021 10:47 am
by 1024MAK
INKEY$ will return the result of the last keyboard scan. This result is updated every time that the machine scans the keyboard. So what is likely happening is that between the time the last IF statement is processed and the LET is processed, the keyboard is being scanned, and the result is no key is detected as being pressed. Hence the VAL will fail as a null string is not a number...

Do as olofsen suggests and at the beginning of your routine capture the current state of the keyboard in a variable. Then only test the value in variable. After the condition testing. Convert the value in the variable.

Mark

Re: Why "C"?

Posted: Thu May 27, 2021 2:10 pm
by LoadError
That makes sense. Thanks.
I'll have to find some more tricks for the 1K version, because the addition of the LET i$=INKEY$ line is enough to cause an out of memory. But I'll manage! :P