Arrays of classes issue

Here's the situation: I am creating a simple Civilization5 simulator (don't ask). I have a class called Tile which contains three ints food, hammer, and gold. There are also two functions assign (which assigns values to the ints) and dispTile, which displays all three values. So far so simple.
_____________________________________________________________________________

class Tile {
public:
int food;
int hammer;
int gold;
void assign(int f, int h, int g) {
food = f;
hammer = h;
gold = g;
}
void dispTile() {
cout << "food: " << food << endl;
cout << "hammers: " << hammer << endl;
cout << "gold: " << gold << endl;
}
};
______________________________________________________________________

In Civ5, tiles are arranged together with each tile occupying a certain locaation. I have chosen to give each tile coordinates x and y to represent that tile's location. For simplicity I have not made x and y members of the class Tile.

I have created a two dimentional array of tile, like this:

x coordinate
|
|
Tile mytile[][]
^
|
|
y coordinate
so tile[2][3] represents the tile with coordinates x = 2 and y = 3

I declare Tile mytile[2][2] i.e. there are nine tiles total:
(0,0) (0,1) (0,2)....(2,1) (2,2)

I use a pair of for loops which run integers x and y from 0 to 2. There are thus 9 combinations of x and y and so tile[x][y] represents 1 of the 9 different tiles declared.

For each tile[x][y] I prompt the user for food, hammer, and gold using cout and cin then I call the function assign to initialize tile[x][y].

Then I display the tile with dispTile().

The last part of the program is the while(1) loop. I prompt for coordinates, then call dislTile(). This allows the user to check that the assigning of values happens correctly.
_______________________________________________________________________

int main() {
int food, hammer, gold;
Tile mytile[2][2]; //Array of tile
int x, y;

for(x = 0; x <= 1; ++x) {
for(y = 0; y <= 1; ++y) {
cout << "\n\ncoords: " << x << " " << y << endl;
cout << "enter food" << endl;
cin >> food;
cout << "enter hammer" << endl;
cin >> hammer;
cout << "enter gold" << endl;
cin >> gold;

mytile[x][y].assign(food, hammer, gold);
mytile[x][y].dispTile();
}
}

while(1) {
cout << "Enter coordinates of tile you want: ";
cin >> x;
cin >> y;
mytile[x][y].dispTile();
}
}

______________________________________________________________

This program compiles properly and when I run it:
bold indicates the stuff I type, normal is the program's output

coords: 2, 1
enter food
2
enter hammer
3
enter gold
6 //assign function called

food: 2 //this is dispTile
hammers: 3
gold: 6

As you can see, assign seems to be working and dispTile is displaying the right values. So far so good. However, when I check tile[2][1] again in the while loop, I get issues:

Enter coordinates of the tile you want: 2 1

food: 2
hammers: 3
gold: 4

What?? The value of "gold" is supposed to be 6, not 4. How did it get changed?

I have noticed a few things which are telling: first of all, only gold gets corrupted. food and hammers works fine. Even more telling, when I drop the for loops and try it for only ONE tile, it works fine:

_________________________________________
int main() {
int food, hammer, gold;
Tile mytile[2][2];
int x, y;

//for(x = 0; x <= 2; ++x) {
// for(y = 0; y <= 2; ++y) {
x = 1;
y = 2;
cout << "\n\ncoords: " << x << " " << y << endl;
cout << "food" << endl;
cin >> food;
cout << "hammer" << endl;
cin >> hammer;
cout << "gold" << endl;
cin >> gold;
mytile[x][y].assign(food, hammer, gold);
mytile[x][y].dispTile();
// }
//}

while(1) {
cout << "Enter coordinates of tile you want: ";
cin >> x;
cin >> y;
mytile[x][y].dispTile();
}
}
________________________________________________________

Running the probram:

coords: 2, 1
enter food
2
enter hammer
3
enter gold
6 //assign function called

food: 2 //this is dispTile
hammers: 3
gold: 6

Enter coordinates of the tile you want: 2 1

food: 2
hammers: 3
gold: 6 //now it's working fine

This suggests that some of the data is being overriten somehow.






closed account (zb0S216C)
Could you please edit your post with code tags. It makes it easier to read.
The index of an array goes from 0 to n-1.
Topic archived. No new replies allowed.