Moving code out of main function

Hi, I was told by a work colleague that the code within the main function should be kept to a minimum. With that in mind, I started trying to move some of the heavier functions away from the main function and call a separate function instead. Only, I'm having a problem getting the function to work as intended, I am pretty sure the problem lies in this section (the whole function is around 180 lines). If it isn't clear in the code, this is part of a shopping section in a game, as the code stands, you are able to buy and sell items, but the gold doesn't change.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
          for (int j=0; j<ITEMSNUMBER; j++)
        {
            if (shopitem==j+1) //the user selects 1 for 0th element etc.
            {
                int afford= MainCharacter.Gold/Items[j].BuyPrice; // this calculates afford correctly
                cout << "How many do you want to buy? (max " << afford << ")" << endl;
                int quantity;
                while(!(cin >> quantity))
                {
                    cerr << "Choice <" << quantity << "> is Invalid, Please Choose a Valid Option\n";
                    cin.clear(); // Clears the input stream fail flag
                    cin.ignore(100, '\n'); // Ignores any characters left in the stream
                }
                if (quantity<=afford)
                {
                    Items[j].Quantity+=quantity; // this quantity rises
                    MainCharacter.Gold-=quantity*Items[j].BuyPrice; //Gold does not go down
                    cout << "Thank you for your purchase" << endl;
                }
                else
                {
                    cout << "Insufficient Gold" << endl;
                }
            }
        }


Sorry for the edit, but I did some debugging, turns out the gold is going down, but it's getting reset for some reason, here's the MainCharacter class (object type Hero).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
class Character
{
public:
    int CharHP;
    int CharMaxHP;
    int CharLevel;
    int CharStrength;
    int CharDefense;
};
class Weapon
{
public:
    std::string WeaponName;
    int WeaponStrength;
    int SellPrice;
    int WeaponQuantity=0;
};

class Hero: public Character, public Weapon
{
public:
    std::string HeroName;
    int Gold=50;
    int TotalExperience=0;
    int ExpNextLevel=10;
    void SetStats(int HP, int MaxHP, int Level, int Strength, int Defense)
    {
        CharHP=HP;
        CharMaxHP=MaxHP;
        CharLevel=Level;
        CharStrength=Strength;
        CharDefense=Defense;
    }

    void SetWeapon(std::string Name="No Weapon", int Strength=10)
    {
        WeaponName=Name;
        WeaponStrength=Strength;
    }

    void Stats()
    {
        using namespace std;
        cout << string(2, '\n');
        cout << this->HeroName << endl;
        cout << "Level: " << this->CharLevel << endl;
        cout << "HP: " << this->CharHP << "/" << this->CharMaxHP << endl;
        cout << "Strength: " << this->CharStrength << endl;
        cout << "Defense: " << this->CharDefense << endl;
        cout << "Exp: " << this->TotalExperience << "/" << this->ExpNextLevel << endl;
    };

    void LevelUp(int load=0)
    {
        this->CharLevel=this->CharLevel+1;
        this->CharMaxHP=this->CharMaxHP+10;
        this->CharStrength=this->CharStrength+2;
        this->CharDefense=this->CharDefense+1;
        using namespace std;
        this->ExpNextLevel=this->ExpNextLevel+(this->CharLevel*this->CharLevel*5); // this should return 10,30,75,155 etc when multiplier is x5
        if (load==0)
        {
            cout << "You levelled up to level " << this->CharLevel << "." << endl;
        }
    };

};
Last edited on
Topic archived. No new replies allowed.