storing a map into 2d char array
Sep 28, 2015 at 2:22pm UTC
I am trying to store a map, which is read from the file "map.txt" into 2d char array.
The problem is though that i am for some reason keep too many spaces, or mess up everything due to the newline character.
the map.txt looks like this
1 2 3 4 5 6 7 8 9 10
10 09
XXXXX
XXXXXX...X
X..J.....X
X..JXXX.XX
XXGJG.G.GX
X.JX....X
X.MXXXXXX
X..X
XXXX
10 and 09 defines the dimensions of the 2d char array, and the rest contains the character including whitespace which has to be stored within this char array.
The way i have stored it now is like this.
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
ifstream inputFile;
int boardSizeRow;
int boardSizeCol;
inputFile.open("map.txt" );
inputFile >> boardSizeCol;
inputFile >> boardSizeRow;
char **gameBoard = new char *[boardSizeCol];
for (int i = 0; i < boardSizeCol; ++i) {
gameBoard[i]= new char [boardSizeRow];
}
cout << "done" << endl;
for (int col = 0; col < boardSizeCol; col++)
{
for (int row = 0; row < boardSizeRow; row++)
{
inputFile >>std::noskipws>> gameBoard[col][row];
}
}
cout<< "done2" << endl;
for (int col = 0; col < boardSizeCol; col++) //////////////TO TEST PRINT
{
for (int row = 0; row < boardSizeRow; row++)
{
cout << gameBoard[col][row];
}
cout << endl;
}
Problem with this method is that when i output it, it doesn't look like the same as the one in the map.txt .
Last edited on Sep 28, 2015 at 2:30pm UTC
Topic archived. No new replies allowed.