1

I have been programming for about 3 weeks now and I'm making this civ game. The only problem is during each round, your stats for civ update each round but after the second round, they do not update. Basically what I want the program to do is add up each of resources after each round and calculate the population and gold, but it's not happening after the first round. I have never gotten classes to work, so don't expect me to get it right the first time.

Here is the code for the update that should happen each round inside the function:

int RoundTotal(int yg, int yk, int yf, int ys, int yr, int yfi,
    int co, int rtp, int gtp, int ap, double tr, int yp, int dp,
    int int yd, double fp) {

    int YourGold = yg, YourStrength = ys, YourKnow = yk, YourFood = yf, 
    YourResource = yr, YourFields = yfi, YourPopulation = yp, YourDefense = yd;

    int ResourceTradeProfit = rtp, GoldTradeProfit = gtp, DroughtProduction = dp;

    int totals, count = co, ArcherPay = ap;
    double taxrate = tr,  FoodProduction = fp;

    if (YourStrength<0) {
        YourStrength = 0;
    }
    FoodProduction = (0.5*YourFields + 0.5*YourKnow - 0.02*YourPopulation)*DroughtProduction;

    YourFood = YourFood + FoodProduction;

    YourGold = YourGold + (taxrate/100)*YourPopulation; 

    YourGold -= (YourStrength / 2);
    YourGold -= YourKnow;
    YourGold -= YourFood;
    YourGold -= ArcherPay;
    YourResource += ResourceTradeProfit;
    YourGold += GoldTradeProfit;

    YourPopulation = YourPopulation + YourFood*FoodProduction;

return totals, YourGold, YourKnow, YourFood, YourStrength,
        YourResource, YourFields, count, ResourceTradeProfit,
        GoldTradeProfit, ArcherPay, taxrate, YourPopulation,
        DroughtProduction, FoodProduction;

Disregard all of the abbreviations for the variables up top, unless they are the problem.

Genghis Khan
  • 19
  • 1
  • 4
  • 2
    What do you expect the return to do? – tkausl Sep 17 '17 at 05:08
  • 1
    Your function is only returning a single `int` so on the calling side of this function you are only getting a single value. From a design side, you should probably create a class that holds all of this data so you can simply return the class or add an update method to the class or something like that. – pstrjds Sep 17 '17 at 05:08
  • 1
    You should probably read about the [comma operator](http://en.cppreference.com/w/cpp/language/operator_other) and this SO post [here](https://stackoverflow.com/q/54142/416574) – pstrjds Sep 17 '17 at 05:11

2 Answers2

3

Your objects are not being updated because you are only passing out a single integer from your method and since you are copying all of the data, all of the operations you are performing in the update function are only operating on the copies and not the original values from the calling side of the operation.

As I mentioned in my comment, you should probably reconsider your design and consider using a class which contains the values you want to update. This answer is not intended to show the "best" design, but it should point you in the right direction. In general, having a method signature that takes more than 3 or 4 arguments is difficult to use and makes reading the code much more difficult (I highly recommend reading Robert Martin's book Clean Code). This is an example of how you could use a class to pass around the necessary data. You may want to make the update function a part of this class. You may also want to consider just passing in the data object as a reference and updating it directly, but that will all depend on your overall design.

Note I didn't test this and may have missed one of your operations in the update method, but hopefully this points you in the right direction.

class YourData
{
    public:
        int Gold;
        int Strength;
        int Know;
        int Food;
        int Resource;
        int Fields;
        int Population;
        int Defense;

        int ResourceTradeProfit;
        int GoldTradeProfit;
        int DroughtProtection;
        double FoodProduction;

        // Normally you would split out the function definitions between
        // a header file and a .cpp file, but for the example I am just
        // putting the code here.
        YourData() {} // Default constructor
        YourData(const YourData& data) // Copy constructor
        {
            Gold = data.Gold;
            Strength = data.Strength;
            // Left out other data members for brevity
        }

        void updateFoodProduction()
        {
            FoodProduction = (0.5 * Fields + 0.5 * Know - 0.02 * Population) * DroughtProduction;
        }
}

YourData roundTotals(const YourData& data, double taxRate, int archerPay)
{
    YourData updated(data);
    if (updated.Strength < 0) updated.Strength=0;

    updated.updateFoodProduction();
    updated.Food += updated.FoodProduction;
    updated.Gold += (taxrate/100) * updated.Population; 
    updated.Gold -= (updated.Strength / 2);
    updated.Gold -= updated.Know;
    updated.Gold -= updated.Food;
    updated.Gold -= archerPay;
    updated.Resource += updated.ResourceTradeProfit;
    updated.Gold += GoldTradeProfit;

    updated.Population += updated.Food * FoodProduction;

    return updated;
}
pstrjds
  • 16,840
  • 6
  • 52
  • 61
  • Everything so far works, except I keep getting an 'unresolved external symbol' error and have no idea how to fix it. – Genghis Khan Sep 17 '17 at 18:54
  • @GenghisKhan - Where are you getting that error, is it with this code or somewhere else? – pstrjds Sep 17 '17 at 21:47
  • In visual studio, I doesn't say a line number for the error. EDIT: I just got it to work, but I got very very large numbers and have no idea what is going on because it runs through the equations but should get way smaller numbers. – Genghis Khan Sep 17 '17 at 21:49
  • @GenghisKhan - If you are having a new/different problem, then you should post a new question with the details of that problem. If one of the provided answers here has answered this question, you should accept one. – pstrjds Sep 17 '17 at 21:58
  • @GenghisKhan - one thought on the "new" problem of big numbers, are you actually initializing all the values in the `YourData` class when it is constructed? I did not write all the code for that, but if you don't initialize them then they will contain undefined values. – pstrjds Sep 17 '17 at 22:06
  • If I don't define the variables in class, then it will give me an error, if I do then I get big numbers. I am not sure if I am initializing it when it is constructed. – Genghis Khan Sep 17 '17 at 22:09
  • @GenghisKhan - if you don't have some lines in the code somewhere (in the constructor, near the time the object is created, etc) where you are setting the initial values of the class, something like `Gold = initialGold;` or `Gold = 0;`, then you probably are not initializing them, but this is a different question than what you asked initially. You should read some tutorials on C++ classes. – pstrjds Sep 17 '17 at 22:18
  • I have initialized them before I call the object, but now the numbers output 0. – Genghis Khan Sep 17 '17 at 22:26
  • @GenghisKhan - You will need to walk through it with the debugger, set some break points, add some watches, add a few print statements, etc. If you are still having trouble then ask a new question including the code where the problem is, what the desired behavior is and what it seems to be doing. – pstrjds Sep 17 '17 at 22:30
0

Instead of trying to return all of the values, which is not an option in C++, you could pass the parameters of the function by reference, so that they are updated even when the function ends. To do this, you simply place the & operator before the name of the variable in the function prototype and definition. For example, the function square, which multiplies an integer by itself and updates the value of that integer:

#include <iostream>
using namespace std;

void square(int &n);

int main()
{
    int i = 4;
    cout << "Before: " << i << endl;
    square(i);
    cout << "After: " << i << endl;

    return 0;
}

void square(int &n) 
{
    n = n * n;
}

Output:

Before: 4
After: 16
Josh Simani
  • 107
  • 10