Diagnol array checker

i have a 8 by 5 grid. i am trying to check if the diagnol line has 5 'X's in a row. im not sure why my code isnt ending when i enter 5 'X's diagnolly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  int diag_checker(char board[][5], char v) {
	int diag_counter = 0;
	int r=0;
	int c=4;
	int o=0;
	while(o<5){
	    
	    if(board[r][c]==v){
	        diag_counter=diag_counter+1;
	    }
	    
	    r=r+1;
	    c=c+4;
	    o=o+1;
	    
	}
	return diag_counter;

}
not sure exactly what you are doing there...
1
2
3
4
5
6
seems like it should be..
bool chk = true;
for(o = 0; o < five; o++) //five is a placeholder for a constant you would define somewhere
  if(board[r++][c++] != v) //assuming a diagonal is r+1 and c+1????? I don't get +4?
    chk = false;
return chk;


I guess I don't get the weird C values. I know you said 8x5 but 5 iterations of +4 is still not a line inside that, its out of bounds which wraps around to a strange location.

why it does not end? Your code will end after 5 iterations. If you have an infinite loop, this is not where the problem lies.

Last edited on
How about adding a debugging printout on line 7:
1
2
std::cout << "Now checking board[" << r << "][" << c
          << "] that has'" << static_cast<int>(board[r][c]) << "'\n";


That output should show which array elements you do check, and what they have.
(I print the char as int, because I have a bad feeling ...)
Topic archived. No new replies allowed.