Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 11:09 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 : Number to string

     Page 1 of 2    
Author Message
Chrisk

Senior Member

Joined: 21/12/2014
Location: Australia
Posts: 122
Posted: 12:26am 03 Aug 2015
Copy link to clipboard 
Print this post

Hi Guys

I have a need to convert a number to a text string.
I thought that this would work however when I run it it get the error message shown below.

CUR=0
Print CUR
If CUR=1 then CUR$ = "open"
else CUR$ = "closed"
Endif
Print CUR$
End




[4] Else CUR$ = "closed"
Error: Unexpected text

Can someone assist please

Regards
Chrisk
 
bigmik

Guru

Joined: 20/06/2011
Location: Australia
Posts: 2950
Posted: 12:33am 03 Aug 2015
Copy link to clipboard 
Print this post

Hi Chrisk,

As anyone here knows, I am not a really good programmer but what if you put your ELSE statement in the same line as your if statement?

I.e. Remove the line feed before ELSE.


You prob need to remove the endif also in my example

Regards,

MickEdited by bigmik 2015-08-04
Mick's uMite Stuff can be found >>> HERE (Kindly hosted by Dontronics) <<<
 
twofingers

Guru

Joined: 02/06/2014
Location: Germany
Posts: 1593
Posted: 12:53am 03 Aug 2015
Copy link to clipboard 
Print this post

Hi Chrisk,

Mick is right!

CUR=0
Print CUR
If CUR=1 then
CUR$ = "open"
else
CUR$ = "closed"
Endif
Print CUR$
End


OR

CUR=0
Print CUR
If CUR=1 then CUR$ = "open" else CUR$ = "closed"
Print CUR$
End


BTW I think it is a good practice to use different var names to make your code more bulletproof.
(e.g. Cur_num and Cur_str$)

Regards
Michael
causality ≠ correlation ≠ coincidence
 
factus10
Regular Member

Joined: 30/12/2014
Location: United States
Posts: 45
Posted: 12:56am 03 Aug 2015
Copy link to clipboard 
Print this post

ChrisK,

You can do this with an array, too.

DIM CURS(1) AS STRING = ("Closed","Open")
cur = 0
print curs(cur)

v4.5
dim cur$(1) length 6
cur$(0) = "Closed"
cur$(1) = "Open"
cur = 0
print cur$(cur)

To fix your code, put the CURS$="closed" on its own line.
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9610
Posted: 01:37am 03 Aug 2015
Copy link to clipboard 
Print this post

  twofingers said  BTW I think it is a good practice to use different var names to make your code more bulletproof.
(e.g. Cur_num and Cur_str$)


Completely agree. Although you probably can use CUR as a numeric variable, and CUR$ as a string, I also think that is a bad idea. While it may well work just fine if MMBASIC does not care, but it would make your code a nightmare to debug, as you would be confusing CUR and CUR$ all the time, and they are two distinctly different variables - not my idea of fun!
Smoke makes things work. When the smoke gets out, it stops!
 
Chrisk

Senior Member

Joined: 21/12/2014
Location: Australia
Posts: 122
Posted: 11:05pm 03 Aug 2015
Copy link to clipboard 
Print this post

Thanks Guys

Some good suggestions here.
I will implement the solution ASAP.
Thanks again for your prompt replies. It was bugging me for ages.

Chrisk
 
Dylan
Regular Member

Joined: 17/06/2013
Location: Netherlands
Posts: 81
Posted: 04:58am 04 Aug 2015
Copy link to clipboard 
Print this post

  Grogster said  
  twofingers said  BTW I think it is a good practice to use different var names to make your code more bulletproof.
(e.g. Cur_num and Cur_str$)


Completely agree. Although you probably can use CUR as a numeric variable, and CUR$ as a string, I also think that is a bad idea. While it may well work just fine if MMBASIC does not care, but it would make your code a nightmare to debug, as you would be confusing CUR and CUR$ all the time, and they are two distinctly different variables - not my idea of fun!


Disagree completely and utterly.

The $ sign is part of the variable name in BASIC. It is pronounced "string". It is natural to use X$ ("x-string") for the string representation of X (a number), in much the same way that it is not natural to use X_str$ ("X underscore <str> string") for a completely unrelated number X_num.

That said, ONLY use the same variable "name" for both numbers and strings if they represent the same thing - as they do here - unless of course you are limited to single characters (A-Z and A$-Z$) which is not the case here.
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10310
Posted: 05:48am 04 Aug 2015
Copy link to clipboard 
Print this post

  Quote  Disagree completely and utterly.


You may disagree but in 4.7 it is not allowed. Like many things this is a judgement call but the issue is that unless you force variable declaration for all variables, i.e.

dim a as integer
dim a$ as string

then it is too easy to type "a" when you intended "a$". Without strict data declaration and typing this doesn't generate an error but just creates a new unintended variable making debugging a nightmare.

For this reason, and with the inclusion of integers since 4.6, as a new datatype, Geoff has now made trying to declare "a" and "a$" in the same program give an error:

"Error: variable or constant already declared"

You can still do what you want but in a more controlled way

DIM myvar_int as INTEGER
DIM myvar_str as STRING

note that you don't have to use $ to make a string but you could equally go

DIM myvar_int%
DIM myvar_str$

 
jeffj
Regular Member

Joined: 04/02/2012
Location: Australia
Posts: 84
Posted: 06:28am 01 Nov 2018
Copy link to clipboard 
Print this post

Hi folks
I am stuck on a string problem
the foll is a test programme for an alarm system
I cannot get the program to print the text of the alarm name
eg when AL(i)=1 I want it to print Battery.I cannot get around mixing up Nos and text
Yr help would be appreciated
jeff



AL1$= "Batty"
AL2$= "NFT flow"
AL3$= "Pump- ExStarts"
AL4$="G House Leak"
dim AL(5)

i=1
let AL(3) =1
do
A=(i)
print "A=";A
B$= str$(A)
print "B=";B$
if AL(i)=1 then print AL;B$;$'This Not ok
print AL1$'This ok

i=i+1
loop until i=4
end
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4044
Posted: 08:08am 01 Nov 2018
Copy link to clipboard 
Print this post

This

if AL(i)=1 then print AL;B$;$'This Not ok

is bad syntax. There is no such thing allowed as $ by itself - see the semi-colon.

It would also fail because AL is an array and you would need to say which element i.e. AL(1) or whatever.

John
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4044
Posted: 08:11am 01 Nov 2018
Copy link to clipboard 
Print this post

More info:

You probably want to put the strings AL1$, AL2$ etc into an array, say name$(4).

Then you can print name$(i) in your loop.

I suggest you either read up more on BASIC syntax or look at sample programs - or both :)

JohnEdited by JohnS 2018-11-02
 
Paul_L
Guru

Joined: 03/03/2016
Location: United States
Posts: 769
Posted: 09:00am 01 Nov 2018
Copy link to clipboard 
Print this post

@Chrisk -- I think you have two separate problems here.

1. You are mixing the single line IF with the multi-line IF. Either form should work.
if CUR=1 then CUR$="open" else CUR$="closed"
or
if CUR=1 then
CUR$="open"
else
CUR$="closed"
endif

When MMBasic sees
If CUR=1 then CUR$ = "open"
it thinks it is reading a single line if construction and it has reached the end of the line and the end of the construction.

Then in the next line it sees
else CUR$ = "closed"
and doesn't know what to do because it isn't expecting an ELSE so it stops and says "Unexpected text".

2. In line 1 you define the variable CUR as either an integer or a float with CUR=0 then, in line 3, you define it again as a string with CUR$="open". MMbasic might accept this, but after the second definition of CUR$ as a string the first definition with CUR=0 will likely cease to exist.

Microsoft BASIC, and its doppelgangers, permitted the simultaneous existence of the same variable name as multiple types, that is A$ (string), A% (integer), A! (16 byte float) and A# (eight byte float) could all exist simultaneously.

In order to simplify the internal variable table Geoff decided not to follow this pattern. You can't use the same variable name as different type variables at the same time. You can, however, add a suffix to the variable name like this.

CURi%=0 : CURf!=22 : CURs$="sss"


@jeffj -- Your code is failing in line 14
if AL(i)=1 then print AL;B$;$

This asks MMbasic to print "AL" which has not been defined, then print "B$" which has been defined, then print "$" which is not a valid variable name but a type designator.

If you're trying to print failure codes based on the setting of an integer try this

' define and fill a message$() array with strings
DIM message$(5) = ("Batty","NFT flow","Pump- ExStarts","G House Leak"," ")
' define a trip array
DIM trip%(5) ' this automatically fills all the elements with zero

' test it
for i%=1 to 5
for j%=1 to 5:trip%(j%)=0:next j% ' set all trip%(1-5) elements to zero
trip%(i%)=1 ' set one trip%() element to 1

' display the results
print "For i = "; i%
for j%=1 to 5
if trip%(j%)=1 then
print " j = "; j%, "TRIP! "; message$(j%)
else
print " j = "; j%, "NO TRIP! "
endif
next j%
next i%
end


Have fun guys.

Paul in NY
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2170
Posted: 12:22pm 01 Nov 2018
Copy link to clipboard 
Print this post

Maybe I have missed this but having gone through the thread, there seems to be a lot of "might be allowed..." etc but no-one seems to have picked up on this...

you can't have CUR and CUR$. It seems the way variable names are handled that the type indicator can be implicit but the name part is absolute.


> clear
> cur=1
> cur$="fred"
Error: CUR already declared
>


I think this is the root of the OP's problemEdited by CaptainBoing 2018-11-02
 
twofingers

Guru

Joined: 02/06/2014
Location: Germany
Posts: 1593
Posted: 03:28pm 01 Nov 2018
Copy link to clipboard 
Print this post

aahemm ...
  CaptainBoing said   I think this is the root of the OP's problem

@CaptainBoing/Andrew

1. This thread is 3 year old.
2. Peter (matherp) explained it (the OP's problem) perfectly (3 years ago).

Sorry!

The second issue (jeffj's) seems also solved.

Best regards
Michael

causality ≠ correlation ≠ coincidence
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2170
Posted: 09:40pm 01 Nov 2018
Copy link to clipboard 
Print this post

LOL!

good catch... I guess I was too intent on reading the thread to notice the dates

Thanks for pointing it out
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9610
Posted: 10:02pm 01 Nov 2018
Copy link to clipboard 
Print this post

Yes, twofingers is correct. This thread is very old now, and A-LOT of development has happened within MMBASIC in the last three years, I am sure most members here would agree.

@ Glenn - Would it be worthwhile locking threads that get this old, and that way, no-one can post on them once they get to a certain age? This would mean any questions about what was in the thread, could be addressed easily with a NEW thread, that points to the old one, so people can go back and read the old thread if they want to.

I probably have missed something obvious here, otherwise old threads WOULD be auto-locked, but.....

The fact that members were caught out by reading the old info and not quite noticing the age of the thread first, is a case in point.
Smoke makes things work. When the smoke gets out, it stops!
 
jeffj
Regular Member

Joined: 04/02/2012
Location: Australia
Posts: 84
Posted: 04:22am 02 Nov 2018
Copy link to clipboard 
Print this post

Thanks Paul for yr prompt reply.
I know it was an old post but it was the only one I could find that was relevant to strings.
I tried to click on "new topic" but it wouldn't work

Paul I am having a problem with line 4 It comes up with the error unable to find variable and I couldn't find how to fix it'
The % in the code has me foxed.Excuse my ignorance

thanks Jeff
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 05:10am 02 Nov 2018
Copy link to clipboard 
Print this post

Jeff,
What device are you programming on - Maximite or micromite etc?
and what version of MMBasic is it running?

There are a lot of differences between the devices and getting advice for the wrong setup will cause much head-banging.

Jim

VK7JH
MMedit
 
jeffj
Regular Member

Joined: 04/02/2012
Location: Australia
Posts: 84
Posted: 08:07am 02 Nov 2018
Copy link to clipboard 
Print this post

Hi Jim V4.5 and mono maximite
Jeff
 
Paul_L
Guru

Joined: 03/03/2016
Location: United States
Posts: 769
Posted: 06:02pm 02 Nov 2018
Copy link to clipboard 
Print this post

  jeffj said  Paul I am having a problem with line 4 It comes up with the error unable to find variable and I couldn't find how to fix it'
The % in the code has me foxed.Excuse my ignorance

thanks Jeff

Hi Jeff,

Line 4 in your code seems to be
AL4$="G House Leak"
Is that the line which produces the error unable to find variable? That doesn't make any sense since line 4 simply defines and initializes a string variable.

A "%" following a variable name defines it as an integer variable, the "$" makes it a string variable, and the "!" makes it a floating point variable which can store decimal points and fractions.

The first time MMBasic sees a variable it will assume it is a floating point variable unless it is told otherwise. You can define variables before using them with the DIM command -- DIM xyz as string -- or -- DIM xyz$. After doing this MMBasic will handle xyz as a string even if you do not include the "$" after the variable name.

I, personally, like to use the type identifiers after each use of a variable like this.
for i%=1 to 10
print i%
next i%

This makes my code look like it's full of fly specs, but it keeps the variables sorted out.

Arrays also have a type property and must be defined with a DIM before being used.
' define and fill a message$() array with strings
DIM message$(5) = ("Batty","NFT flow","Pump- ExStarts","G House Leak"," ")
' define a trip array
DIM trip%(5) ' this automatically fills all the elements with zero

Loops work faster when the counter variable is an integer.
for i%=1 to 10000:print i%;:next i% ' integer counter

if faster than
for i=1 to 10000:print i;:next i ' float counter


Have fun.

Paul in NY
Edited by Paul_L 2018-11-04
 
     Page 1 of 2    
Print this page
The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025