Why "C"?

Anything Sinclair ZX Basic related; history, development, tips - differences between BASIC on the ZX80 and ZX81
Post Reply
User avatar
LoadError
Posts: 22
Joined: Thu Apr 29, 2021 12:45 pm

Why "C"?

Post 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?
My Sinclair belongings:
- unexpanded, unmodified, unbackporched ZX81
- expanded (from 16K to 48K) rubber keyed ZX Spectrum
- a Cambridge Scientific pocket calculator
olofsen
Posts: 189
Joined: Wed Jan 08, 2014 12:29 pm

Re: Why "C"?

Post by olofsen »

Perhaps it is best to call INKEY$ once

Code: Select all

LET I$ = INKEY$
and test only I$ in the subsequent code.
User avatar
siggi
Posts: 988
Joined: Thu May 08, 2008 9:30 am
Location: Wetterau, Germany
Contact:

Re: Why "C"?

Post 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
My ZX81 web-server: online since 2007, running since dec. 2020 using ZeddyNet hardware
http://zx81.ddns.net/ZxTeaM
User avatar
1024MAK
Posts: 5103
Joined: Mon Sep 26, 2011 10:56 am
Location: Looking forward to summer in Somerset, UK...

Re: Why "C"?

Post 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
ZX81 Variations
ZX81 Chip Pin-outs
ZX81 Video Transistor Buffer Amp

:!: Standby alert :!:
There are four lights!
Step up to red alert. Sir, are you absolutely sure? It does mean changing the bulb :!:
Looking forward to summer later in the year.
User avatar
LoadError
Posts: 22
Joined: Thu Apr 29, 2021 12:45 pm

Re: Why "C"?

Post 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
My Sinclair belongings:
- unexpanded, unmodified, unbackporched ZX81
- expanded (from 16K to 48K) rubber keyed ZX Spectrum
- a Cambridge Scientific pocket calculator
Post Reply