Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 11:08 01 Aug 2025 Privacy Policy
Jump to

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.

Forum Index : Microcontroller and PC projects : INKEY$ PROBLEM?

Author Message
drkl

Senior Member

Joined: 18/10/2015
Location: Hungary
Posts: 102
Posted: 09:28am 18 Dec 2015
Copy link to clipboard 
Print this post

HELO,
Here a short code:
DO
L$=INKEY$
IF L$="" THEN ? "x"
ELSE
? L$
ENDIF
PAUSE 1000
LOOP

Running on MM5.0, I allways get x, typing anything.
What is the mistake?

drkl
 
palcal

Guru

Joined: 12/10/2011
Location: Australia
Posts: 1993
Posted: 09:53am 18 Dec 2015
Copy link to clipboard 
Print this post

Hi Drkl,
Try this

DO
L$ = Inkey$
Loop until L$ <> ""
Print L$

Paul.
"It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all"
 
drkl

Senior Member

Joined: 18/10/2015
Location: Hungary
Posts: 102
Posted: 10:05am 18 Dec 2015
Copy link to clipboard 
Print this post

Hi Paul,
not good. the result as above.

drkl
 
palcal

Guru

Joined: 12/10/2011
Location: Australia
Posts: 1993
Posted: 10:12am 18 Dec 2015
Copy link to clipboard 
Print this post

Hi Drkl,
How can it be ' as above ' there is no X in my code.
I tested it and it returned the key I hit.
PaulEdited by palcal 2015-12-19
"It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all"
 
drkl

Senior Member

Joined: 18/10/2015
Location: Hungary
Posts: 102
Posted: 10:19am 18 Dec 2015
Copy link to clipboard 
Print this post

Hi Paul,

I apologize for the inaccuracy, but when I run the code
and no matter what I type, nothing is displayed on the screen.

drkl
 
twofingers

Guru

Joined: 02/06/2014
Location: Germany
Posts: 1593
Posted: 10:24am 18 Dec 2015
Copy link to clipboard 
Print this post

plz try this:

Do
L$=Inkey$
If L$="" Then
Print "x"
Else
Print L$
EndIf
Pause 1000
Loop


Michael
causality ≠ correlation ≠ coincidence
 
palcal

Guru

Joined: 12/10/2011
Location: Australia
Posts: 1993
Posted: 10:25am 18 Dec 2015
Copy link to clipboard 
Print this post

Drkl,
What setup are you using, Maximite, Micromite......?
Paul.
"It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all"
 
drkl

Senior Member

Joined: 18/10/2015
Location: Hungary
Posts: 102
Posted: 11:16am 18 Dec 2015
Copy link to clipboard 
Print this post

Hi Paul

44 pin Micromite with V5.0 firmware

drkl

 
ztoti
Regular Member

Joined: 27/10/2011
Location: Canada
Posts: 65
Posted: 11:49am 18 Dec 2015
Copy link to clipboard 
Print this post

Hi Drkl,
This is almost same like your program, but it works :)
DO
L$=INKEY$
IF L$="" THEN
? "x"
ELSE
? L$
ENDIF
PAUSE 1000
LOOP


ZoranEdited by ztoti 2015-12-19
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9610
Posted: 01:28pm 18 Dec 2015
Copy link to clipboard 
Print this post

As others have hinted at, the problem is with your code syntax.

DO
L$=INKEY$
IF L$="" THEN ? "x"
ELSE
? L$
ENDIF
PAUSE 1000
LOOP

With multi-line IF-THEN, the code needs to go to the next line after the THEN word - just as twofingers and ztoti have shown.

It's an easy mistake to make, as standard one line IF/THEN, is, by it's very nature, one line.

Multi-line IF/THEN is structured just a little different.

MMBasic looks at the routine, sees it is a multi-line with ENDIF statement, so ignores anything after the word THEN on the first line, and starts executing what is on the next line(as the IF/THEN is true).

As there is nothing to execute between that line and ELSE, it just executes the ELSE code, which is always going to be an "x" no matter what.

Isn't writing code fun?(rhetorical)

EDIT: Actually, my description of how that works is actually wrong. You should techically not be even getting the "x", but by structuring the code as twofingers and ztoti have shown, it does work(I have also tried it), but I am actually a little mystified myself as to why that "x" is showing at all.... Oh well.Edited by Grogster 2015-12-19
Smoke makes things work. When the smoke gets out, it stops!
 
Geoffg

Guru

Joined: 06/06/2011
Location: Australia
Posts: 3292
Posted: 02:15pm 18 Dec 2015
Copy link to clipboard 
Print this post

Grogs, you almost got it right in your explanation.

What happened is that the interpreter hit the IF statement and found that it was a one line IF command so it then printed or did not print the "x" as required. The interpreter then continued and hit the ELSE statement. Now the code following the ELSE is only executed if there was a previous multiline IF that failed and because that had not happened it skipped to the ENDIF and then continued.

The problem is really one of error checking. When the interpreter hit the ELSE it should have scanned back through the code to check if the previous IF was a multiline IF and thrown an error if it was not. But that would considerably slow down every program that uses a multiline IF.

The IF command's syntax is convoluted and difficult to implement in an interpreter and I can only blame Bill Gates. He devised the syntax when he wrote the first incarnation of Microsoft Basic as a 20 year old. On the whole he did a brilliant job but this was not one of his finest moments.

Geoff
Geoff Graham - http://geoffg.net
 
robert.rozee
Guru

Joined: 31/12/2012
Location: New Zealand
Posts: 2442
Posted: 05:08pm 18 Dec 2015
Copy link to clipboard 
Print this post

would it be sufficient for the interpreter to set a flag called 'MultiLineIf' when it sees an "if" without a "then" on a single line, and clear the flag when it sees an "if" and a "then" on the same line?

if the interpreter then sees an "else" it checks the flag 'MultiLineIf' and throws a syntax error if the flag is not set.

to allow for nested "if" statements it may be necessary to maintain a stack for 'MultiLineIf'. re-reading through the above, it seems convoluted, but may help inspire a better solution.


cheers,
rob :-)
 
Geoffg

Guru

Joined: 06/06/2011
Location: Australia
Posts: 3292
Posted: 01:40am 19 Dec 2015
Copy link to clipboard 
Print this post

  robert.rozee said  to allow for nested "if" statements it may be necessary to maintain a stack for 'MultiLineIf'

I knew that someone would suggest this...

It gets more complicated the more you delve into it. A stack helps but then you need some way of unwinding the stack when someone uses goto or exit sub to disrupt the program's flow. And the intent of all this complication is just trying to catch one syntax error - and there are thousands of syntax errors that are possible.

In short, error checking is difficult in BASIC.

Geoff
Geoff Graham - http://geoffg.net
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 10:05am 19 Dec 2015
Copy link to clipboard 
Print this post

Code verification is something that I have considered in MMEdit.
I found it difficult to be sure that my idea of correct is the same as MMBasic's idea of correct/acceptable.

For now, using the code format tool highlights some likely suspects for errors.

In this case:


The DO ... LOOP doesn't line up.

I find the indenting invaluable as a quick check in MMBasic as well as Liberty Basic and PureBasic, my two 'other loves'.

Jim


VK7JH
MMedit
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025