0
enum { WOOD, BRICK, GRAIN, HEMP, WOOL, RAWMETAL, HONEY, SALT, METALGOODS, MEAD, CLOTH, BEER, STOCKFISH, CLOTHING, CHEESE, PITCH, PELTS, MEAT, WINE, SPICES, TOTALRESOURCES };
// An array of strings for each of the resource names
// As long as these are in the same order as the enum everything should match up
// .: resName[PIGIRON] is "Pig Iron"
string resName[]{ "Wood", "Brick", "Grain", "Hemp", "Wool", "Raw Metal", "Honey", "Salt", "Metal Goods", "Mead", "Cloth", "Beer", "Stockfish", "Clothing", "Cheese", "Pitch", "Pelts", "Meat", "Wine", "Spices" };
enum { Edinburgh, Scarborough, Boston, London, Bruges, Haarlem, Nimwegen, Groningen, Cologne, Minden, Bremen, Erfurt, Hamburg, Lubeck, Rostock, Berlin, Ripen, Flensburg, Aalborg, Naevsted, Bergen, Stavanger, Oslo, Stockholm, Gothenburg, Malmo, Ahus, Visby, Stettin, Posen, Breslau, Danzig, Thorn, Warsaw, Konigsberg, Kaunas, Riga, Reval, Helsinki, Novgorod, TOTALTOWNS};
string townName[]{ "Edinburgh", "Scarborough", "Boston", "London", "Bruges", "Haarlem", "Nimwegen", "Groningen", "Cologne", "Minden", "Bremen", "Erfurt", "Hamburg", "Lubeck", "Rostock", "Berlin", "Ripen", "Flensburg", "Aalborg", "Naevsted", "Bergen", "Stavanger", "Oslo", "Stockholm", "Gothenburg", "Malmo", "Ahus", "Visby", "Stettin", "Posen", "Breslau", "Danzig", "Thorn", "Warsaw", "Konigsberg", "Kaunas", "Riga", "Reval", "Helsinki", "Novgorod"};
class resource
{
public:
    float demand, production, businessNeeds, businessProduction;

    // This function, called a constructor, is run every time a new resource is created
    // In this case, it assigns 0 to everything
    resource()
    {
        demand = 0;
        production = 0;
        businessNeeds = 0;
        businessProduction = 0;
    }
    float net()
    {
        return (this->production - this->demand);
    }
    float businessNet()
    {
        return (this->businessProduction - this->businessNeeds);
    }
};

class town
{
public:
    // The array of pointers to each of a our resource objects
    resource *resList[TOTALRESOURCES];

    // This is the town constructor
    town()
    {
         // Loops through the array and creates a new resource object in each
         // the resource constructor assigns the default values of 0.
        for (int i = 0; i < TOTALRESOURCES; i = i + 1)
        {
            resList[i] = new resource();
        }
    }

    ~town()
    {
    // Loops through the array and deletes each resource object
    for (int i = 0; i < TOTALRESOURCES; i = i + 1)
    {
    delete resList[i];
    }
};

int main()
{
    //What do I do here?
    for (int i = 0; i < TOTALTOWNS; i++)
    {
         town townName[i];
    }
    system("pause");
    return 0;
}

So, I'm a software engineering student and I just switched majors so I decided to take some personal time to learn how to code in c++ a bit better. I decided to build a program that can plan the logistics for a videogame called Patrician IV.

I have put about 5 days worth of work into this project and have found some serious problems with my initial code (hard to add new functionality and change stuff around). So I took a step back and am trying to build my classes in a more succinct manner while also being able to loop through each instance of town later in a loop so I can update the demand, production, businessNeeds, and businessProduction values easily. I was copying and pasting about 6 lines of code 40 times before this.

I want to know:

(A) is it possible to do what I want - i.e. can I use enums and a for-loop to construct instances of town.

(B) how to loop through each of the towns so that I can add values to the resource variables.

(C) a third-grade-level explanation of how to use pointers for similar purposes would also be great.

:) THANK YOU!

  • What is the objective of the program? What kind of input it is expected to take? What is it supposed to produce as output? – R Sahu Oct 29 '15 at 04:52
  • `resource *resList[TOTALRESOURCES];` could be `resource resList[TOTALRESOURCES];` to avoid manual memory management, and respect rule of 0/3/5 – Jarod42 Oct 29 '15 at 08:38
  • The program is supposed to read in the demand and production values for both population and businesses in towns from a .csv and output the locations and amounts of resources that I need to load and unload through another .csv file. – Timothy Law Oct 29 '15 at 13:40

2 Answers2

0

In your main function, use the same idea as resList to initialize your town objects, so:

town* townName[TOTALTOWNS]
for (int i = 0; i < TOTALTOWNS; i++)
{
     townName[i]= new town();
}

Then, I'm guessing you want to give different values for each of the different resources. Switch statements go along with enums well. So I would recommend something like this:

for (int i = 0; i < TOTALRESOURCES; i = i + 1)  
{
    switch(i)
    {
        case WOOD:
            townName[EDINBURGH]->resList[WOOD]->demand= yourValue;
            break;
        case BRICK:
            break;

    }
}

Or if you're cycling through towns:

for (int i = 0; i < TOTALTOWNS; i = i + 1)  
{
    switch(i)
    {
        case EDINBURGH:
            break;      //etc.
    }
}

If you only want to update a single town or resource, depending on how you're organizing everything, you could create something like this. A function that takes your town array and the enum indexes, like this:

updateTownResources(town* (*townName)[TOTALTOWNS], int townEnum, int resourceEnum, int dValue, int pValue )
{
    townName[townEnum]->resList[resourceEnum]->demand= dValue;
    townName[townEnum]->resList[resourceEnum]->production= pValue;
    //etc...
}
jas
  • 69
  • 4
  • You should pass array by reference instead of by pointer according to provided code. – Jarod42 Oct 29 '15 at 08:36
  • Thanks so much! I got it to work, it took me a while to realize I have to use townName[1]->resList[1]->demand to call the demand of the second towns second resource. Should work beautifully. – Timothy Law Oct 29 '15 at 13:46
  • @Jarod42 You're right about the enums, sorry about that. I've edited that part out. – jas Oct 29 '15 at 17:48
-1
int main()
{
    std::vector<town*> townArray;
    //What do I do here?
    for (int i = 0; i < TOTALTOWNS; i++)
    {
         town* pTown = new(std::nothrow) town;
         townArray.push_back (pTown);
    }
    std::vector<town*>::iterator iter = townArray.begin();
    for (;iter != townArray.end(); iter++) {
        (*iter); // gives access to individual objects
    }
    system("pause");
    return 0;
}
Nandu
  • 808
  • 7
  • 10