need some help

#include <iostream>
#include <cstdlib>

using namespace std;

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;
};

struct enemy
{
int warriors;
int race;
int gold;
};

int tutorial();
int play();

int main()
{
int x =0;
int y =0;
cout << "Hello and welcome to the game of town building!\n";
cout << "1. Tutorial\n";
cout << "2. Play game\n";
cout << "3. Quit\n";
cout << "Please insert choice here:";
cin >> x;
system("cls");
if (x == 1)
{
tutorial();
}
else if (x == 2)
{
play();
}
else if (x == 3)
{
cout << "Now, exiting thanks for playing.";
exit(0);
}
else
{
main();
}
//else if (x)=anything else display please insert a choice from 1 2 or 3 add +1 to y each time
//if y ==5 then tell them the game is to difficult for them to understand and exit.

return 0;
}
int tutorial()
{
cout << "Hello and welcome to the tutorial\n";
cout << "This will explain how to play the game\n";
cout << "In this game you command a small village\n";
cout << "When you first start the game you will see a list\n";
cout << "You can choose 1 of 3 races\n";
cout << "You can choose 1 of 3 starting conditions\n";
cout << "Other than that it should be self explanetory\n";
system("pause");
system("cls");
main();


}

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


stats[1].food = 200;
stats[1].wood = 200;
stats[1].water = 200;
stats[1].silver = 200;
stats[1].gold = 200;
//stats[1].civs = 100;
stats[1].warriors = 20;
//stats[1].houses = 4;
//stats[1].weapons = 100;
//stats[1].armor = 100;
//stats[1].ammo = 200;
stats[1].race = 0;
stats[1].start = 0;
stats[1].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[1].race;
if (stats[1].race == 1)
{
cout << "You have chosen the human race is that correct?\n";
cin >> x;
if (x == 'y')
{
stats[1].food = (stats[1].food + 200);
stats[1].water = (stats[1].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[1].gold << endl;
cout << "Silver: " << stats[1].silver << endl;
cout << "Food: " << stats[1].food << endl;
cout << "Water: " << stats[1].water << endl;
cout << "Warriors: " << stats[1].warriors << endl;
cout << "Wood: " << stats[1].wood << endl;
cout << "Level: " << stats[1].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[1].race == 2)
{
stats[1].gold = (stats[1].gold +200);
stats[1].silver = (stats[1].silver + 200);
}
else if (stats[1].race == 3)
{
stats[1].warriors = (stats[1].warriors + 50);
}

}


My question is, when i run it stats[1].wood comes out as a big number 2030043336 when it should be 200. Any ideas?
stats only has one element, so stats[1] is out of bounds.
so can you explain to me how every other stats[1]."" comes out just fine?
Thanks Athar, it works now.
thread can be closed.
As for why only wood is changed - it's undefined behavior, anything can happen.
The more practical answer is that due to the way the stack typically works, stats[1].wood overlaps with the memory occupied by the variable x - so changing x also "changes" the non-existing stats[1].wood.
Last edited on
closed account (o3hC5Di1)
Hi there,

Try replacing stats[1] by stats[0] everywhere, except for the declaration of that array.
Why are you declaring an array of just one element in the first place, why not just create a single town?

It's funny to me that it would compile like this - stats[1] would clearly be out of bounds as Mr. Athar said.
Arrays are zero index, which means the first element is accessed by array[0].
In the declaration town stat[1] you indicate the amount of items in the array, so that's a different thing.

Hope that helps.

All the best,
NwN
Topic archived. No new replies allowed.