String manipulation
James Newman
Posts: 133
I've got a section of code that isn't working as it should. Can anyone spot my problem? The following is the offending code, it should allow numeric characters to be appended to a string, and backspace should remove the last character of the string.
I'm using the standard keyboard object, and inputString is defined in the Var section as "long inputString[noparse][[/noparse] 2 ]"
Ohhh and i'm displaying it via FloatString objects .str. When I do, I get garbage.
Post Edited (James Newman) : 12/9/2007 8:57:06 AM GMT
I'm using the standard keyboard object, and inputString is defined in the Var section as "long inputString[noparse][[/noparse] 2 ]"
key := keyboard.key case key "0".."9": i := 0 'Numbers 0-9 repeat _MAX_INPUT*4-1 if byte[noparse][[/noparse] @inputString ][noparse][[/noparse] i ] == 0 byte[noparse][[/noparse] @inputString ][noparse][[/noparse] i ] := key quit i++ $C8: i := 0 'Backspace repeat _MAX_INPUT*4 i++ if byte[noparse][[/noparse] @inputString ][noparse][[/noparse] i ] == 0 byte[noparse][[/noparse] @inputString ][noparse][[/noparse] i-1 ] := 0 quit
Ohhh and i'm displaying it via FloatString objects .str. When I do, I get garbage.
Post Edited (James Newman) : 12/9/2007 8:57:06 AM GMT
Comments
(2) The main question is: Where is the memory for your string?
When you say: "LONG inputString" this is only part of the truth...
(a) if you assign the address of the string-buffer (but where is it?) to this variable, you obviously have to use
If you - erronoiusly - use the LONg as string-buffer, that your code could work, but you are limited to 3 characters.
I think you are not fully aware that you have to allocate memory for any "string" you use.
(3) Make shure the string-buffer is cleared each time time you start anew.
To be honest, it's not clear from the code what you intend it to be doing.
is quite all right, too
deSilva> The string is allocated as a long in the var section, and is 2 longs in size
Edited the above code so that it looks better, ohhh and _MAX_INPUT is the amount of -longs- allocated for the string, 2 atm.
Post Edited (James Newman) : 12/9/2007 8:59:12 AM GMT
Ohhh, got it.
Thought about it, and I shouldn't have the '@' symbols. Without them, it works... I had tried without before, but at that time some other things where different.
Well, thanks anyway, I'll be back when I hit my next stumble.
Post Edited (James Newman) : 12/9/2007 9:54:21 AM GMT
Thank you.
If I can't figure it out soon, I'll make a full program that shows the problem,and post it. Thanks in advance.
EDIT: Ok, well finally got it figured out. I went back and declared the inputString variable as a byte array insead of a long. Declaring it as a long had given me working results because when I went to draw the string, I wasn't passing the address of inputString, I was passing the value (which would have been the first character). The only reason this worked is because in one test I had put 'inputString = string("Test"). This ofcourse was an error because it was assigning the address of 'string("Test") to the first long in 'inputString'. Which allowed display.str to work since it requires an address. I had tried several methods for changing the string, and most where valid, the problem was infact mostly in my displaying of the string.
Well, that code wasn't posted, so you all didn't have much of a chance to help me I would have posted that, but it's pretty complicated because I've written my own string drawing 'helper' routines.
deSilva> Just wanted you to know that I did read everything in the thread, problem was that I had tried everything mentioned, or was already aware of it. Had I posted all of the code, someone might have seen my problem, but it has alot of asm mixed in, and would have complicated things more. Thanks for trying to help though, maybe next time I'll have all the information posted. Probley won't happen though, as I rarely post problems with programming. The only reason I posted this was because I'm on a tight dealine.
Anyway, problem solved, everything above is pretty much useless. I was doing 'display.str(inputString)' instead of 'display.str(@inputString)'.
Post Edited (James Newman) : 12/9/2007 11:31:11 PM GMT
There were many possibilities how you could have defined your string buffer, if any.
But it is good to hear you now understand the way adressing and dereferencing works. That is of fundamental importance for any program of intermediate complexity...
I am not sure wehat you describe as a "mistake" is really wrong. Some of the things you mention are quite valid, but have to be continued consequently...