While loop not updating...Why?

Started by , Fri 20/05/2005 02:55:19

Previous topic - Next topic

monkey0506

I have the following while loop that keeps producing an error saying it's not updating:

Code: ags
while (ibuf < lenbuf) {
      StrFormat(output, "%s%c", output, StrGetCharAt(obuf, ibuf));
      ibuf++;
      }


ibuf is a char defined directly before this while loop as:

Code: ags
char ibuf = 0;


lenbuf is a char defined for previous use and reappointed as:

Code: ags
lenbuf = StrLen(output) - 1;


output is clearly a string, as you can tell by this point.  The problem is that even though the limits are defined as "ibuf is less than lenbuf" and ibuf is updated once per loop, the game still says that it isn't updating...I have other lines of code similar to this that don't appear to be producing errors, but this one doesn't seem to be working... ???

Gilbert

#1
Try putting a Display("%d", lenbuf); just after assigning its value to see what value it's actuallyÃ,  set to.

Didn't notice any main problem with it, but even though string lengths are supposed to be at most ~200, it's not recommended to use char typed variables for running indices (even if you want to save memory), you shall try using short or int for ibuf and lenbuf instead.

One problem that I can see is, in:
lenbuf = StrLen(output) - 1;
If output is empty, it's supposed to be 0 - 1 = -1, right?
However, the behaviour is not like that, since char variables are unsigned, it's actually warped to 255 (if it's short or int, it will be set correctly to -1, so don't use char variables for this kind of things unles syou know clearly what you want).
I don't see how this can be related to your problem though (as 255 loops is okay), but it'll be quite possible thatÃ,  StrGetCharAt(obuf, ibuf)); may generate some other error since it can read past the length of the string obuf. (Also, did you check that your code is valid only if
length of obuf >= length of output - 1
?)
EDIT:Just checked the manual, StrGetCharAt() won't generate an error if you read past the string.
Another possibility is that you didn't actually declare lenbuf as char, but as short or int, so it can be >255, and for some unknown reasons, it's indeed set to something > 255, in that case the loop will ne an "infinite" one, because of the warping behaviour of the char ibuf and that it can't get past 255.

monkey0506

Converting them both to shorts took care of the problem, though I don't see why...I used a char because strings can only contain up to 200 characters and chars contain values from 0 - 255...

Gilbert

Yeah, but for reasons I had mentioned, use char for calculations/indices, etc. only if you're really sure they won't mess things up.
Can't see (yet) why it won't work in your case though, I had done some small tests with those codes but they didn't give me such error, maybe it's because I hadn't tested all the variable value combinations.

Good to hear that your problem is solved.

SMF spam blocked by CleanTalk