Tad confused on constants.

I have a text file with a format like this.

3 6
X X X O O O
O X X O O O
X X X X X X


The first two numbers are the size of a two dimensional array. Since I need constant integers for two dimensional arrays to even function how would I pull out those numbers and then assign them to a constant int? I have tried multiple ways that I know of, but the compiler will not accept them.
Last edited on
You could do a dynamic array (2d c-string) or just use c++ strings (std::string).
You can't. The whole point of constant ints is that they are constant. IE, they cannot be assigned.

Since the size of this array is not known at compile time, you will need to dynamically construct the array.

There are several ways to do this. The easiest is to use a vector:

1
2
3
4
5
6
7
8
9
10
11
#include <vector>

...

int width, height;
 // .. get width/height from your file

std::vector<std::vector<char>>  data( height, std::vector<char>(width) );

// now you can use 'data' as you would a normal 2D array:
data[y][x] = 'X';


Though I am not a fan of multidimensional arrays. If you are interested, I wrote this a while back explaining why I avoid them:

http://www.cplusplus.com/forum/articles/17108/#msg85594
I know you cannot change the constants as soon as they are declared, but is there no way to declare constant with the information after reading the file and obtaining those numbers?
For it to be constant, the value has to be known at compile time.

The size of your array cannot be known at compile time because you are getting it from the file at runtime.


So no. There is no way to do what you are suggesting. You need to create the array dynamically. Or use some other dynamically sizable container (like string) as giblit suggested.
Topic archived. No new replies allowed.