1
void GameBoard::print(const GameBoard& computerBoard) { Grid[0][0] = '1'; Grid[0][1] = '2'; Grid[1][0] = '3'; int i, j; int sides = SIZE; cout << " Your bombs: Your navy:" << endl << endl; for(i=0; i<SIZE; i++) { // prints your bombs cout << sides << " "; for(j=0; j<SIZE; j++) { cout << computerBoard.Grid[i][j] << " "; } cout << " "; // prints your ships cout << sides << " "; for(j=0; j<SIZE; j++) { cout << Grid[i][j] << " "; } cout << endl << endl; sides--; } j = 0; cout << "\n "; for(i=0; i<SIZE; i++) { j = i + 'A'; cout << (char)j << " "; } cout << " "; for(i=0; i<SIZE; i++) { j = i + 'A'; cout << (char)j << " "; } cout << endl << endl; 

}

I am constructing a game like battleship, and need to change the for loop to read..

for(i=8;i>0;i--) 

why does this produce an error?

Sorry if this doesnt make sense, Grid[0][0] should be on the bottom left, but it is currently at the top left.

3
  • 2
    What error is being produced? Also, if you're in the for loop that you want (i=8...) are you calling these locations in Grid? If so, array indices 0-8 must exist, so it must be type Grid[9] or whatever. Commented Feb 28, 2011 at 1:00
  • I don't see anywhere in your main code containing that line. That single line looks fine to me unless variable i wasn't declared beforehand. Commented Feb 28, 2011 at 1:01
  • BTW try not to use the 'magic number' Commented Feb 28, 2011 at 1:02

1 Answer 1

8

I guess what you want is something like

for(i=SIZE-1; i>=0; i--) 

because this is equivalent to

for(i=0; i<SIZE; i++) 

both of these go from 0..SIZE-1 while

for(i=SIZE; i>0; i--) 

goes from 1..SIZE (and so if you access an array of length SIZE using arr[i], will produce an error).

Sign up to request clarification or add additional context in comments.

4 Comments

Im making an 8 x 8 grid, grid[0][0] is on the top left. I need it to be on the bottom left position.
The last loop would work if you accessed index i - 1, but that would be ugly.
@Marlon: Nice catch, edited the answer to make this a bit clearer.
@bluetickk: If the code you posted is the one where grid[0][0], it should help to replace for(i=0; i<SIZE; i++) with for(i=SIZE-1; i>=0; i--) to reverse the output vertical.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.