ok so ive tried doing this but it doesnt seem to work... I'm coding a game and whn i code the output it would be a lot easier just to have a for() statement that would loop until it equaled a certain alue... can this be done.. 2-d array, also it compiles fine but when it gets to the part that it uses the array wih variable my i get a windows error that says program is has stopped working
thanks cj
somthing like this...

Code:

//not actual code
int x;
int y;
array[x][y];
cout<< array[x][y];

12-06-2009

Daved

There are two issues in your example code. First, you aren't allowed to use variables like x and y as the size of your array in standard C++. It might work on your compiler if you use g++ because they allow it as an extension. If that's the case you can use it, but be aware it won't work on all compilers.

The standard way to do a dynamic array like that is to use a vector (or in this case a vector of vectors). They are designed to have a size that is determined at runtime. If you're not able or not allowed to use vector, you can also use a regular array with a max size, then just keep track of how many actual values you are using.

Your example code doesn't initialize x and y, and it uses array[x][y] which is out of bounds of the array. I assume you're aware of those issues, at least the first, but it sounds like you're tying to figure out how to loop through all values? For a 2-D array, you just need a nested for loop. The first starts at zero and runs while the index is less than x, the second runs from 0 to y.

12-06-2009

jamort

yeah... meant that i already had the size of the array.. and was just going to use the x and y in the for loop... which works is really good being that it cut out about 400-500 lines of code... by the way im working on a connect four game