Hello :)

I'm making a (new :P) game in C++, and in the beginning of the file, I'm initializing an array like this

int tower_dmg[5][3];
int tower_spd[5][3];
int tower_eff[5][3];
int tower_efc[5][3];
int tower_cst[5][3];

And later in the program, when loading images and initializing a lot of stuff, i want to fill these up. But the only way I can make it work is by entering each value as tower_dmg[0][0] = #; tower_dmg[0][1] = #; tower_dmg[...], which is going to take a lot of annoying space in the code and is taking a long time to write (and i'm lazy ^_^), so i though, isn't there a way that you can initialize an array later using the method you can when first initializing them? (int tower_dmg[5][3] = {{#,#,#}, {#,#,#} [...]})

(sry if you don't understand, I'm not good at explaining things, hope you understand, otherwise just ask and I'll try again :P)

If reading the values from a file you can use loops to fill up the arrays.

If reading the values from a file you can use loops to fill up the arrays.

can i do this without making the user able to alter the file?

I don't know. Too vague of a question.

ok thx anyways :)

Not sure whether this is what you are after, but from this line:

isn't there a way that you can initialize an array later using the method you can when first initializing them? (int tower_dmg[5][3] = {{#,#,#}, {#,#,#} [...]})

I figured I'd post this, though it sounds like you already know how to do this and the rest of your post seems to suggest that this is not your situation. I figured I'd post it anyway. Like AD mentioned, you may have to be more specific.

#include <iostream>

int main()
{
    int tower_dmg[5][3] = {{1,2,3},{4,5,6},{7,8,9},{10,11,12},{13,14,15}};
    
    for(int i = 0; i < 5; i++)
    {
         for(int j = 0; j < 3; j++)
         {
              std::cout << tower_dmg[i][j] << '\t';
         }
         
         std::cout << std::endl;
    }
    
    return 0;
}

If you start with say int tower_dmg_original[5][3] = {{1,2,3},{4,5,6},{7,8,9},{10,11,12},{13,14,15}}; Then later on, you could do this

int tower_dmg[5][3];
for ( int r = 0 ; r < 5 ; r++ ) {
  for ( int c = 0 ; c < 3 ; c++ ) {
    tower_dmg[r][c] = tower_dmg_original[r][c];
  }
}

Since you have 5 separate arrays all of the same size, make it a function.

If you start with say int tower_dmg_original[5][3] = {{1,2,3},{4,5,6},{7,8,9},{10,11,12},{13,14,15}}; Then later on, you could do this

int tower_dmg[5][3];
for ( int r = 0 ; r < 5 ; r++ ) {
  for ( int c = 0 ; c < 3 ; c++ ) {
    tower_dmg[r][c] = tower_dmg_original[r][c];
  }
}

Since you have 5 separate arrays all of the same size, make it a function.

Or better yet, you could do something like...

typedef int tower[5][3] = {{1,2,3},{4,5,6},{7,8,9},{10,11,12},{13,14,15}};

tower tower_dmg_original, tower_spd, tower_eff, tower_efc, tower_cst;

Now all of your towers will be initialized.

wow really nice :D
now I also learned typedef ^_^

Thx for all the replies :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.