structures

Question, yeah, it's me again.

Is there any way to add structures together?

for example, i have;
struct town
{
int wood;
int stone;
int food;
int water;
int silver;
int gold;
int civs;
int warriors;
int houses;
int weapons;
int armor;
int ammo;
int race;
int start;
int level;
int choicex;
int workone;
int worktwo;
int workthree;
int workfour;
};

and then i have it insalized in two different areas like so:

int play()
{
char x;
town stats[1];


stats[0].food = 200;
stats[0].choicex = 0;
stats[0].wood = 200;
stats[0].water = 200;
stats[0].silver = 200;
stats[0].gold = 200;
//stats[1].civs = 100;
stats[0].warriors = 20;
//stats[1].houses = 4;
//stats[1].weapons = 100;
//stats[1].armor = 100;
//stats[1].ammo = 200;
stats[0].race = 0;
stats[0].start = 0;
stats[0].level = 1;
cout << "So you've decided to play the game eh?\n";
cout << "Let get started then...\n";
cout << "What race would you like to be?\n";
cout << "1. Human You start off with more water and food.\n";
cout << "2. Orc You start off with more gold and silver.\n";
cout << "3. Undead You start off with more warriors.\n";
cin >> stats[0].race;
if (stats[0].race == 1)
{
cout << "You have chosen the human race is that correct?\n";
cin >> x;
if (x == 'y')
{
stats[0].food = (stats[0].food + 200);
stats[0].water = (stats[0].water + 200);
system("pause");
system("cls");
cout << "So as it stands now I understand that you have chosen Humans as your race\n";
cout << "Now i shall tell you your resource count\n";
cout << "Gold: " << stats[0].gold << endl;
cout << "Silver: " << stats[0].silver << endl;
cout << "Food: " << stats[0].food << endl;
cout << "Water: " << stats[0].water << endl;
cout << "Warriors: " << stats[0].warriors << endl;
cout << "Wood: " << stats[0].wood << endl;
cout << "Level: " << stats[0].level << endl;
system("pause");
}
else
{
cout << "I'm sorry, guess i heard you wrong, lets try again.\n";
cout << "The screen will now clear.\n";
system("pause");
system("cls");
play();
}
}
else if (stats[0].race == 2)
{
stats[0].gold = (stats[0].gold +200);
stats[0].silver = (stats[0].silver + 200);
}
else if (stats[0].race == 3)
{
stats[0].warriors = (stats[0].warriors + 50);
}

//choicex starts here.
system("cls");
todo();
that's one here is the second one;

int todo()
{

town test[1];
test[0].choicex = 0;
test[0].workone = 0;
cout << "So now you have a few starting materials\n";
cout << "Lets get the game started\n";
cout << "You have a few choices now\n";
cout << "1. Go out into the forest and chop some wood\n";
cout << "2. Go down to the river and get some water to place in your well\n";
cout << "3. Go into the mines and try to find some ore\n";
cout << "4. Go work on the farms for some food\n";
cin >> test[0].choicex;
if (test[0].choicex == 1)
{
cout << "You have chosen to walk out into the woods and chop a tree down\n";
stats[0].wood = stats[0].wood + 200;

}


}

how would i add them, if it's even possible. better yet take the number from the function above it and have them be able to move that number back and forth.
Last edited on
No that is not possible (short of manually assigning individual elements).

What you should be doing is using the same object in both areas. You can do this by passing the 'town' (oddly named class) to your 'todo' function as a parameter by reference:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void todo(town& t)
{
  // set the remaining members of 't' here
  t.bar = whatever;
}

int main()
{
  town mytown;

  // initialize members of 'mytown' here
  mytown.foo = whatever;

  // hand it off to 'todo' to have it do the rest
  todo( mytown );  // this will actually modify 'mytown'
}
Last edited on
Topic archived. No new replies allowed.