Reading txt into 2D Array
Feb 19, 2013 at 11:49pm Feb 19, 2013 at 11:49pm UTC
My assignment for class is to create a program like Life. In the program, we have to read a txt file into a 2D array that acts as the map for the game. The txt file looks like this...
1 2 3 4 5 6 7 8 9 10 11 12
OOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
O............................O
O............................O
O............................O
O..........XX................O
O..........XX................O
O............................O
O............................O
O............................O
O............................O
O............................O
OOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
...where 'O' are the boundaries, '.' are dead cells, and 'X' are live cells.
My code looks like...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
{
int rows=0, cols;
in_file.open("text.txt" );
if (in_file)
{
getline(in_file, gstring, '\n' );
while (in_file)
{
for (cols=0; cols<COLS_SIZE; cols++)
{
char *ptr = &gstring[cols];
array1[rows][cols]=*ptr;
}
getline(in_file, gstring, '\n' );
rows++;
}
}
}
My code compiles, but keeps giving me a "string subscript out of range" error within the program run time.
Last edited on Feb 19, 2013 at 11:50pm Feb 19, 2013 at 11:50pm UTC
Topic archived. No new replies allowed.