Well, I was trying to remember how to do multidimensional arrays.
I typed in what I felt I'd memorized, but I had a little important typo.
See code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
#include <iostream>
using namespace std;
int main()
{
const int ROWS = 3;
const int COLUMNS = 3;
char board[ROWS][COLUMNS] = {{'X', 'O', 'X'},
{'O', 'X', 'O'},
{'X', 'O', 'X'}};
cout << "Printing the board\n";
for(int i = 0; i < ROWS; ++i)
{
for(int j = 0; i < COLUMNS; ++j)
{
cout << board[i][j];
cout << endl;
}
}
system("PAUSE");
return 0;
}
| |
Please ignore the system"(PAUSE"); it's just there so I can see what it does. Besides, it's not a program for use for anything. I was just trying to remember the array like I said.
But anyway the error is in the second for-loop.
instead of:
1 2 3 4 5
|
for(int j = 0; j < COLUMNS; ++j)
{
cout << board[i][j];
cout << endl;
}
| |
I added "i" instead of "j" at a rather crucial point.
for(int j = 0; i < COLUMNS; ++j)
Running this with the "j" in place, it works fine.However, with the "i" there it produces a very heavy crash. This isn't so much a call for help, since I know how to fix it. But I'm curious about what happens when it compiles like that and runs, but produces such wierd results.
I hope someone understands what I mean by my question.
Thanks for any replies.