0

I spent some time on this but I can't figure out why it's not exiting the for loop.

for (int i = 1; i < rows - 1; i++) {
     printf("before copy\n");

     copyArray(rows, columns, table, temp);
     printf("after copy\n");

           for (int j = 0; j < columns; j++) {
              printf("inside j\n");
              if (table[i][j] == xOrO && table[i + 1][j] == xOrO && table[i - 1][j] == '.') {
                  temp[i - 1][j] = xOrO;
                  if (guessTwoMoves(rows, columns, temp, xOrO, playerPiece)) {
                      copyArray(rows, columns, temp, table);
                      return ('A' + j);
                  }
              }
           }

     printf("before break\n");
     printf("%i \n", i);
           if (i == 4) break;

     printf("inside i\n");
}

     printf("outside 3rd");

This is what I get: The i = 4 but it doesn't exit the loop for some reason Check the image to see the output. I am still not used debugging in the terminal so I used print statements. Please check the picture below

Output after debuging

Biffen
  • 6,249
  • 6
  • 28
  • 36
Takobell
  • 23
  • 8
  • 3
    Why do you think it is not exiting the loop? The last thing I see in your output is `4`, which should be the `printf` right before the `break` statement. – Nico Schertler Sep 17 '19 at 03:42
  • 4
    The `printf` statement outside your outer loop (the one with `"outside 3rd"`) doesn't have a newline in it, so it might be still in the IO buffer. Add a newline there or flush the output buffer to see it. If that's not the problem, we'll likely need some more context. – Carl Norum Sep 17 '19 at 03:44
  • Carl Norum, Thanks for pointing it out. after adding the new line it seems that it did exit the loop. I ll try to see what's wrong with it – Takobell Sep 17 '19 at 03:58
  • OK; I'll see if I can find a question to dupe this one to. – Carl Norum Sep 17 '19 at 03:59
  • 1
    Thanks again, I fixed the problem. I was stuck as I thought that the problem was in that loop. I didn't know that I had to add a new line to take out of the buffer – Takobell Sep 17 '19 at 04:09

1 Answers1

0

From what I see is that the "Inside i" is missing on the image that you have shared so the loop actually got terminated.

As for the printf outside of the loop, see the comments to your post.

Robidu
  • 576
  • 3
  • 17
  • Oops, sorry, I have missed the one and considered that to be outside of the scope of the text snippet. My bad... Post got corrected now. – Robidu Sep 17 '19 at 03:57