Function Left$( str$,n )?

BlitzMax Forums/BlitzMax Beginners Area/Function Left$( str$,n )?

mudcat(Posted 2005) [#1]
what is wrong with my code?Trying to get a grasp on string functions.
Graphics 800,600
Global text:String
Global a:Int

Repeat
a=GetChar()
text:+Chr(a)
If KeyDown(key_backspace)
	 text=Left(text,2)  'does this mean the left two characters?
EndIf
DrawText text,100,100
Flip;Cls
Until KeyDown(key_escape)


Believe it's something with my code.When I press backspace,text comes up blank.

Thanks,
mudcat


Michael Reitzenstein(Posted 2005) [#2]
What's happening here is that GetChar is returning 0 for the most part, so you're adding a non printable character to the string every time the code loops, unless you did hit a button.

So if you run the program and type 'asdf', the text will actually be something like this:

********************************a***s***d***f

Where * represents a non printable character. Text will simply skip over them, so when you print that string it'll look just like 'asdf'. But, when you grab the left two characters, they are included, so the result will be '**', i.e. two garbage characters that when you print them out, it will look like an empty string.

To fix it, check that GetChar( ) actually returns a character:

Graphics 800,600
Global text:String
Global a:Int

Repeat
a=GetChar()

'*** change
If a
	
	text:+Chr(a)
	
EndIf

If KeyDown(key_backspace)
	 text=Left(text,2)  'does this mean the left two characters?
EndIf
DrawText text,100,100
Flip;Cls
Until KeyDown(key_escape)



mudcat(Posted 2005) [#3]
Michael Reitzenstein,
Thank you so much,I scratched my head raw on this.

Thanks,
mudcat


SSS(Posted 2005) [#4]
Also, as per your use of the Left command there's a small error. The two means that you're grabbing the first two letters from the left. So, if you had the string 'fasdfsd' Left('fasdfsd',2) would give you 'fa'. If you want it to take bits off of your string one by one you would use Left(text,text.length-1) this means that you would be getting back the string minus the last character


mudcat(Posted 2005) [#5]
sss,
I understand that,but my code wasn't working so I reduced left down to basics to make sure my len(text)-1 command wasn't causing my problems.Thanks though.
Is the commands text.length and text.high and such documented?If so where at?I have trail version.

And I have another problem with same code.If I type letters all the way across the screen and use backspace to erase them,by first holding down a letter,then holding down the backspace,after doing this for about 5 or six times,I recieve the error.

unhandled exception:Attempt to index array element beyond length.



I've tried limitting my length like this.
Graphics 800,600
Global text:String
Global a:Int

Repeat
a=GetChar()
If a 
	If text.length<10 text:+Chr(a)
EndIf
If KeyDown(key_backspace)
	 text=Left(text,text.length-1)  
EndIf
DrawText text,100,100
Flip
Cls
Until KeyDown(key_escape)


still does it.The blue highlight is on Cls.

Thanks
mudcat


klepto2(Posted 2005) [#6]
Take a look at this topic : http://www.blitzbasic.com/Community/posts.php?topic=54410

The error is because of a static KeyBuffer(actual an array) but there is a workaround for it (only if you could rebuild the modules)