![]() |
Forum Index : Microcontroller and PC projects : Unexpected Text
Page 1 of 2 ![]() ![]() |
|||||
Author | Message | ||||
palcal![]() Guru ![]() Joined: 12/10/2011 Location: AustraliaPosts: 1993 |
I am trying to make a small change to some code and have this error [34] Else Rain = Cint(Rain) Error: Unexpected text: Rain Æ ‡Rain) I was using GFX term so I tried MMEdit with the same result does anyone know what is causing this? edit...I even tried using a different computer to do the edit and got the same error. "It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all" |
||||
seco61 Senior Member ![]() Joined: 15/06/2011 Location: AustraliaPosts: 205 |
Hi. Without seeing the rest of the code, it would appear that it does not like the text after the ELSE command. The text displayed in the message looks like the "tokenised" version of "Rain = Cint(Rain)". Have you got the formatting for the IF THEN ELSE correct? (Hard to tell without a bit more code visible - or what the before and after code looks like ![]() Regards Gerard (vk3cg/vk3grs) |
||||
palcal![]() Guru ![]() Joined: 12/10/2011 Location: AustraliaPosts: 1993 |
What I am trying to do is, if the rain value is less that 10 print to 1 decimal place else no decimal place. A larger snippet of code If Rain < 10 Then Rain = Cint(Rain*10)/10 Else Rain = Cint(Rain) EndIf R$=Str$(rain,3) This fails with the error, the coding is also wrongly formatted Str$(Rain,3) would not give the decimal place So I changed the way I did things to this If Rain <10 Then R$ = Str$(Rain,1,1) Else R$ = Str$(Rain,3) EndIf This also fails with this error [34] Else R$ = Str$(rain,3) Error: Unexpected text: R$ Æ ¤rain,3) "It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all" |
||||
palcal![]() Guru ![]() Joined: 12/10/2011 Location: AustraliaPosts: 1993 |
OK I have it working I changed the code so that ELSE is on a separate line. I thought it was OK to have Else R$ = Str$(Rain,3) all on one line. I now have Else R$ = Str$(Rain,3) and it works OK "It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all" |
||||
PeterB Guru ![]() Joined: 05/02/2015 Location: AustraliaPosts: 655 |
G'Day palcal It's nice to see someone else tripping over the IF THEN ELSE MAYBE thing. I have been using BASIC in various forms for many years and I still get it wrong. I think the rules keep changing. Peter |
||||
Grogster![]() Admin Group ![]() Joined: 31/12/2012 Location: New ZealandPosts: 9610 |
Yes, the ELSE needs to be on a line of its own. I'm a little late to this thread, so you have it sorted now, but the interpreter does not seem to really like having commands after the ELSE keyword. I also had this problem, but ever since I have put the ELSE on its own line, I have never had that problem again. ![]() Smoke makes things work. When the smoke gets out, it stops! |
||||
palcal![]() Guru ![]() Joined: 12/10/2011 Location: AustraliaPosts: 1993 |
Thanks to all. "It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all" |
||||
seco61 Senior Member ![]() Joined: 15/06/2011 Location: AustraliaPosts: 205 |
If you want to have the code on the same line as the IF THEN ELSE, the following format will also work (note: there is no ENDIF). If Rain < 10 Then R$ = Str$(Rain,1,1) Else R$ = Str$(Rain,3) Regards Gerard (vk3cg/vk3grs) |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6283 |
You could also do a single line by using a : Else : R$ = Str$(Rain,3) That is a method I often use when I have a lot of elseif's and simple code following. Jim VK7JH MMedit |
||||
palcal![]() Guru ![]() Joined: 12/10/2011 Location: AustraliaPosts: 1993 |
OK thanks. "It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all" |
||||
Geoffg![]() Guru ![]() Joined: 06/06/2011 Location: AustraliaPosts: 3292 |
There is ELSEIF or ELSE IF Geoff Graham - http://geoffg.net |
||||
PeterB Guru ![]() Joined: 05/02/2015 Location: AustraliaPosts: 655 |
Good morning Since we are admitting confusion about IF etc, something like this had me wacked a few weeks ago. I eventually gave up with the excuse that it was too slow for what I wanted but it still bugs me. A = 2 B = 22 If A = B Then GoTo X Else Print "NOT EQUAL" End If GoTo OUT X: Print "EQUAL" OUT: It does not print NOT EQUAL If I change B to 2 it prints EQUAL I know, "WHEN ALL ELSE FAILS, TRY READING THE INSTRUCTIONS" BUT I DID. Peter |
||||
Turbo46![]() Guru ![]() Joined: 24/12/2017 Location: AustraliaPosts: 1642 |
Hi Peter, The manual says about your IF statement: 'This type of IF statement is all on one line' I would have expected then that the ELSE statement would have thrown an error? I'll Try it out later when I can. Bill Keep safe. Live long and prosper. |
||||
Turbo46![]() Guru ![]() Joined: 24/12/2017 Location: AustraliaPosts: 1642 |
I ran the program and got the same result as you. The lines following the IF statement are not being executed when the IF condition is not true. The manual also says: So I expect that it should be either: THEN or GOTO and having both might confuse the interpreter so I removed the THEN from the statement: and got the same result. It does not seem to be working as I expected. This may be a question for Geoff. This version works as expected: Bill Keep safe. Live long and prosper. |
||||
seco61 Senior Member ![]() Joined: 15/06/2011 Location: AustraliaPosts: 205 |
Hi. If the IF command has an expression following the THEN then it is considered to be an IF statement with all parts on the same line. As you have a GOTO following the THEN (or just a GOTO without the THEN), this is considered a one line IF statement. The ELSE on the following line will tried to be matched to a preceding IF - but as it cannot find a match it just ignores the statements from the ELSE to the ENDIF. Some options for writing the statement: A = 2 B = 22 If A = B Then GoTo X Else Print "NOT EQUAL" GoTo OUT X: Print "EQUAL" OUT: A = 2 B = 22 If A = B Then GoTo X Else Print "NOT EQUAL" Endif GoTo OUT X: Print "EQUAL" OUT: Regards Gerard (vk3cg/vk3grs) |
||||
Turbo46![]() Guru ![]() Joined: 24/12/2017 Location: AustraliaPosts: 1642 |
Hi seco61, Yes, as above, the IF statement is considered as a single line statement but as you say the ELSE on the following line will be tried to be matched to a preceding IF. As there isn't one, wouldn't you expect it to throw an error rather that ignore the following lines? I would still expect: IF A = B GOTO X to work. Bill Keep safe. Live long and prosper. |
||||
PeterB Guru ![]() Joined: 05/02/2015 Location: AustraliaPosts: 655 |
G'Day seco61 & Bill Your second example looks a lot like mine except GOTO has been moved down. The manual says the THEN statement can be replaced by GOTO???? My code is only meant to display the problem. The actual code was full of IFs etc. But it does seem to have demonstrated some confusion about this area. Peter |
||||
seco61 Senior Member ![]() Joined: 15/06/2011 Location: AustraliaPosts: 205 |
Hi Peter and Bill. My understanding is that "IF expr GOTO label" is also considered a one line IF statement, hence the same behaviour. As regards reporting an error, it is a LONG time since I actually looked at the interpreter code, but I know being able to report this as an error could be quite difficult when you consider the sequential nature of the interpreter and the ability for code to be GOTO'd etc... Regards Gerard (vk3cg/vk3grs) |
||||
PeterB Guru ![]() Joined: 05/02/2015 Location: AustraliaPosts: 655 |
To me it reads as a multiline IF statement. The only complication is the GOTO but since it is not working, my interpretation is a bit sus. Thank heavens for Cab. Sav. Peter |
||||
PeterB Guru ![]() Joined: 05/02/2015 Location: AustraliaPosts: 655 |
Good morning sec061 & Bill I bet you thought I had gone away ![]() Your second bit runs but when equal it runs once and when not equal runs continuously. Why am I not surprised? I got out my David I Schneider Handbook of BASIC 1988. It only confuses me. I am plucking up the courage to insert a GOSUB / RETURN just for fun. Peter |
||||
Page 1 of 2 ![]() ![]() |
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |