Reputation: 511
I am attempting to take the values of a text file and load it into a 2D array. The problem I am running into is that the value of the char variable only seems to overwrite every position blank and I'm not sure why.
So I have the file open, I can access it to read it's contents or display them on the screen, and set up my 2D array like so:
char chessBoards[BOARD_SIZE - 1][BOARD_SIZE - 1] = {{'A'}}; // All elements of 2D array initialized
int x = 0; // line position - which line we are looking at
int y = 0; // row position - which row we are looking at
Bit in this test the output is all C characters with a single D character where I later told it to be, so my problem seems to be that the positions of the text file are not being copied to the char variable.
file.open(games);
char point = 'Z';
while (file.get(point))
{
for (int x = 0; x < BOARD_SIZE; x++)
{
for (int y = 0; y < BOARD_SIZE; y++)
{
chessBoards[x][y] = point;
}
}
}
chessBoards[1][1] = 'D';
cout << chessBoards[1][1];
for (int x = 0; x < BOARD_SIZE; x++)
{
for (int y = 0; y < BOARD_SIZE; y++)
{
cout << "chessBoards[" << x << "][" << y << "]: ";
cout << chessBoards[x][y] << endl;
}
}
But in this variation every value is blank except for the one 'D'
file.open(games);
char point = 'Z';
while (file.get(point))
{
for (int x = 0; x < BOARD_SIZE; x++)
{
for (int y = 0; y < BOARD_SIZE; y++)
{
chessBoards[x][y] = 'C';
}
}
}
cout << chessBoards[1][1];
for (int x = 0; x < BOARD_SIZE; x++)
{
for (int y = 0; y < BOARD_SIZE; y++)
{
cout << "chessBoards[" << x << "][" << y << "]: ";
cout << chessBoards[x][y] << endl;
}
}
Which tells me that it's not properly getting the values from the file to begin with, but in this version there is no problem displaying the contents of the file on top. However, if I try to put the other stuff in that same area it will only overwrite the first position of the array and stop.
file.open(games);
char point = 'Z';
while (file.get(point))
{
for (int x = 0; x < BOARD_SIZE; x++)
{
for (int y = 0; y < BOARD_SIZE; y++)
{
chessBoards[x][y] = 'C';
}
}
chessBoards[x][y] = 'C';
chessBoards[x][y] = point;
cout << point;
}
chessBoards[1][1] = 'D';
cout << chessBoards[1][1];
Upvotes: 1
Views: 84
Reputation: 63471
Let's start here with two errors in one line:
char chessBoards[BOARD_SIZE - 1][BOARD_SIZE - 1] = {{'A'}};
First, you must use BOARD_SIZE
(not BOARD_SIZE-1
) for your dimension. This will allow indexing between 0 and BOARD_SIZE-1
inclusive. You had undefined behaviour in your loops.
Since you have been writing off the end of the array, you could be trashing other variables in the stack, and anything could happen. Declare the board like this:
char chessBoards[BOARD_SIZE][BOARD_SIZE];
Second, that initialisation does not initialise every element to 'A'
. It only initialises the first, and then zeros every other element. You want something like std::fill_n
(from <algorithm>
):
std::fill_n( (char*)chessBoards, BOARD_SIZE * BOARD_SIZE, 'A' );
Or just memset
(oldschool styles):
memset( chessboards, 'A', sizeof(chessBoards) );
Now the actual board reading. You have been rewriting the entire board for every character. That means when your while
loop finally finishes, the board would contain the very last character that was read. This is probably a new-line, and so later outputting the board it would appear empty.
Essentially, you're reading from the file in the wrong part of the loop. Do this instead:
file.open( games );
if( file.is_open() )
{
for( int x = 0; x < BOARD_SIZE; x++)
{
for (int y = 0; y < BOARD_SIZE; y++)
{
char point = 'C';
if( !file.get( point ) )
std::cout << "Error reading " << x << "," << y << std::endl;
chessBoards[x][y] = point;
}
}
}
Upvotes: 2
Reputation: 133
Well, clearly if you never write chessBoards[x][y] = point;
, you can't expect it to load the points. This applies to your first 2 snippets (which are identical). I'm assuming you accidentally copy-pasted your code twice, since they're exactly the same. It's actually pretty confusing.
Your third one has x
and y
as loop variables; they're out of scope by the time chessBoards[x][y] = point;
is performed. It looks like you're declaring x
and y
twice, once near the top of this code and once in the for
. If that's the case, you shouldn't be writing for (int y = 0; y < BOARD_SIZE; y++)
; you probably want for (y = 0; y < BOARD_SIZE; y++) // note the missing 'int'
instead.
The following program demonstrates the issue:
#include <iostream>
using namespace std;
int main()
{
int x = 0;
int y = 0;
for(int x = 0; x < 2; x++) {
for(int y = 0; y < 2; y++) {
cout << "x: " << x << ", y: " << y << endl;
}
}
cout << "\nx: " << x << ", y: " << y << endl;
return 0;
}
This outputs:
x: 0, y: 0
x: 0, y: 1
x: 1, y: 0
x: 1, y: 1
x: 0, y: 0
Clearly, re-declaring x
and y
in for
is problematic. I'm pretty sure it's not an error because they're in different scopes, but I'm not 100% on this particular scoping rule ... mostly because what you're doing with the loop variables doesn't make sense to do in a program. If it was me, I'd probably just put chessBoards[x][y] = point;
in the inner loop, since I think that's what you're after.
Edit to add: paddy is correct that if you want an 8-element array, you need to declare it like array[8]
, not array[7]
.
Upvotes: 0