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.
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