![]() |
Forum Index : Microcontroller and PC projects : INKEY$ PROBLEM?
Author | Message | ||||
drkl![]() Senior Member ![]() Joined: 18/10/2015 Location: HungaryPosts: 102 |
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: AustraliaPosts: 1993 |
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: HungaryPosts: 102 |
Hi Paul, not good. the result as above. drkl |
||||
palcal![]() Guru ![]() Joined: 12/10/2011 Location: AustraliaPosts: 1993 |
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. 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: HungaryPosts: 102 |
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: GermanyPosts: 1593 |
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: AustraliaPosts: 1993 |
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: HungaryPosts: 102 |
Hi Paul 44 pin Micromite with V5.0 firmware drkl |
||||
ztoti Regular Member ![]() Joined: 27/10/2011 Location: CanadaPosts: 65 |
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 Zoran |
||||
Grogster![]() Admin Group ![]() Joined: 31/12/2012 Location: New ZealandPosts: 9610 |
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. Smoke makes things work. When the smoke gets out, it stops! |
||||
Geoffg![]() Guru ![]() Joined: 06/06/2011 Location: AustraliaPosts: 3292 |
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 ZealandPosts: 2442 |
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: AustraliaPosts: 3292 |
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: AustraliaPosts: 6283 |
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 |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |