While loop error (array index out of bounds) (SOLVED)

Started by monkey0506, Tue 05/04/2005 23:29:34

Previous topic - Next topic

monkey0506

I have two while loops that are causing me some heart-ache:

Code: ags
  while ((i > -1) && (dlgnum[dialog].optstate[i] != 0)) { i--; }
  while ((j < 30) && (dlgnum[dialog].optstate[j] != 0)) { j++; }


The problem is that the game appears to be checking the whole while statement, even if the first half proves to be false.  I need to run through these two variables where they are within the bounds defined (i > -1 and j < 30) and the optstate variable with index i/j does not equal zero.  If either statement proves false I have to break immediately.  Any help greatly appreciated.

BTW, AGS 2.7 Beta...


monkey0506

So...I'm basically screwed until this gets fixed?

strazer

#3
Try something like

Code: ags

  while (i > -1) {
    if (dlgnum[dialog].optstate[i] != 0) i--;
    else i = -1;
  }


Edit:

Or, since I reckon you need "i" later on, something like this:

Code: ags

  int break = 0;
  while ((i > -1) && (break == 0) {
    if (dlgnum[dialog].optstate[i] != 0) i--;
    else break = 1;
  }

monkey0506

Actually I was thinking about it last night and I think I'm going to try this:

Code: ags
short break = 0;
short i = dlgVariable(0) - 1;
while ((dlgnum[dialog].optstate[i] != 0) && (break == 0)) { /* option i not off by scrolling */
  if (i > -1) i--;
  else break = 1;
  }
if (break == 1) break = 0;
short j = dlgVariable(1) + 1;
while ((dlgnum[dialog].optstate[j] != 0) && (break == 0)) { /* option j not off by scrolling */
  if (j < 30) j++;
  else break = 1;
  }


I'm actually looking for the first member of the optstate array (starting at the top shown option - 1 and going backwards (for i) and starting at the bottom shown option + 1 and going forwards (for j)) that is set to 0.  Actually, now that I read your second bit of code I think they would do the same thing...  Thanks for the help.

SMF spam blocked by CleanTalk