String to integer casting problem

BlitzMax Forums/BlitzMax Programming/String to integer casting problem

Can somebody tell me what is the problem with the following code ...

Strict

Global s$=""
Global s2$="10"

Graphics 800,600

While Not KeyHit(KEY_ESCAPE)
	Cls
	s:+Chr(GetChar())
	DrawText "string s:"+s ,20,20
	DrawText "integer s:"+Int(s),20,40
	
	DrawText "string s2:"+s2 ,20,60
	DrawText "integer s2:"+Int(s2),20,80
	Flip
Wend


when I enter numbers they are added to string but this string can not be converted to integer.

Jure

You're adding the chr(0) character when no key is pressed, and that acts like a string termination on the string to integer conversion. Max Strings are not null-terminated, but as BlitzMax modules internally deal with C and C++ code, sometimes the null character chr(0) in a string can affect the code. This is something to keep on mind while coding on BlitzMax.

Fix:
Strict

Global s$=""
Global s2$="10"

Graphics 800,600

While Not KeyHit(KEY_ESCAPE)
	Cls
	Local Char:Int = GetChar()
	If Char <> 0 Then s:+Chr(Char)
	DrawText "string s:" + s, 20, 20
	DrawText "integer s:" + Int(s), 20, 40
	
	DrawText "string s2:"+s2 ,20,60
	DrawText "integer s2:"+Int(s2),20,80
	Flip
Wend


EDIT: Actually I don't think this was the problem. The problem is that the conversion ends when a non numeric character is reached and chr(0) is not a number character.

hey, thank you,
I doubt I would think of that...

Jure