Program hangs on function call

Hi guys. First post here :). I decided to give C++ a try a few days ago, dive in and just see how it went. I managed to get through ok with the help of the cpp manual and some googling, but this problem has me stumped. I tried to implement levelling into my game, and now the game hangs when the player is supposed to level up. Hope it's not too much code for this post. Before it gets asked, I'm using Code::Block to write it.

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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
  #include <iostream>
class Character
{
public:
    int CharHP;
    int CharMaxHP;
    int CharLevel;
    int CharStrength;
    int CharDefense;
};
class Weapon
{
public:
    std::string WeaponName;
    int WeaponStrength;

};

class Hero: public Character, public Weapon
{
public:
    std::string HeroName;

    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=0)
    {
        WeaponName=Name;
        WeaponStrength=Strength;
    }

    void Stats()
    {
        using namespace std;
        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;
    };

    // game hangs when this function gets called:
    void LevelUp()
    {
        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*10); // this should return 10,50,140 etc.
        cout << "You levelled up to level" << this->CharLevel << "." << endl;
    };

};

class Monster: public Character
{
public:
    std::string MonsterName;

    void SetStats(int HP, int MaxHP, int Level, int Strength, int Defense)
    {
        CharHP=HP;
        CharMaxHP=MaxHP;
        CharLevel=Level;
        CharStrength=Strength;
        CharDefense=Defense;
    }

    void Scan()
    {
        using namespace std;
        cout << "Level: " << this->CharLevel << endl;
        cout << "HP: " << this->CharHP << endl;
        cout << "HP: " << this->CharHP << "/" << this->CharMaxHP << endl;
        cout << "Strength: " << this->CharStrength << endl;
        cout << "Defense: " << this->CharDefense << endl;
    };

};
static int Damage (int strength, int defense)
{
  return 3.5*strength/defense;
};

static int Experience (int CharLevel, int EnemyLevel)
{
    return (EnemyLevel*EnemyLevel/CharLevel)+9;
}
using namespace std;
int main()
{
    std::cout << "What is your character's name?" << std::endl;
    std::string HeroName;
    cin >> HeroName;

    Hero MainCharacter;
    MainCharacter.HeroName= HeroName;
    MainCharacter.SetStats(20, 20, 1, 5, 5);
    MainCharacter.SetWeapon();
    // set up an example monster to test fighting.
    Monster Rat;
    Rat.MonsterName= "Rat";
    Rat.SetStats(10, 10, 1, 3, 3);

    bool GameActive=true;
    while (GameActive==true)
    {
        cout << "What do you want to do?" << endl;
        cout << "exit- quit program" << endl;
        cout << "stats- view character stats" << endl;
        cout << "fight- battle a monster" << endl;
        std::string command;
        cin >> command;
        if (command=="exit")
        {
            GameActive=false;
        }
        else if (command=="stats") // display character's stats
        {
            MainCharacter.Stats();
        }
        else if (command=="fight")
        {
            //TO DO: SET UP A BATTLE!
            cout << "Who do you want to fight?" << endl;
            cout << "1- Rat" << endl;
            int choice;
            cin >> choice;
            switch (choice)
            {
                case (1):
                cout << "You see a rat and decide to kill it." << endl;
                while (MainCharacter.CharHP>0 && Rat.CharHP>0)
                {
                    cout << "attack- attacks your target" << endl;
                    cout << "scan- see enemy stats" << endl;
                    cout << "run- ends battle immediately" << endl;
                    std::string attack;
                    cin >> attack;
                    if (attack=="attack")
                    {
                        cout << "You attacked the rat for " << Damage(MainCharacter.CharStrength, Rat.CharDefense) << " damage." << endl;
                        Rat.CharHP-=Damage(MainCharacter.CharStrength, Rat.CharDefense);
                        if (Rat.CharHP>0)
                        {
                            cout << "The rat bit you for " << Damage(Rat.CharStrength, MainCharacter.CharDefense) << " damage." << endl;
                            MainCharacter.CharHP-= Damage(Rat.CharStrength, MainCharacter.CharDefense);
                            if (MainCharacter.CharHP<=0)
                            {
                                cout << "Game Over" << endl;
                                GameActive=false;
                                break;
                            }
                        }
                        else
                        {
                            cout << "You killed the rat." << endl;
                            cout << "You gained " << Experience(MainCharacter.CharLevel, Rat.CharLevel) << " Experience." << endl;
                            MainCharacter.TotalExperience+=Experience(MainCharacter.CharLevel, Rat.CharLevel);
                            // Check if the main character has gained enough exp to level up. This should really be a
                            // while incase of multiple level ups, but that hangs too.
                            if (MainCharacter.TotalExperience>=MainCharacter.ExpNextLevel)
                            {
                                MainCharacter.LevelUp();
                                cout << "Test" << endl;
                            }
                        }
                    }
                    else if (attack=="scan")
                    {
                        Rat.Scan();
                    }
                    else if (attack=="run")
                    {
                        cout << "You successfully ran away." << endl;
                        Rat.CharHP=Rat.CharMaxHP;
                    }
                }
                if (Rat.CharHP<=0)
                {
                    Rat.CharHP=Rat.CharMaxHP;
                }

                break;

                default:
                break;
            }
        }
        else
        {
            cout << "Please enter a valid command" << endl;
        }
    }
    cin.clear();
    cin.ignore(255, '\n');
    cin.get();
    return 0;
}
closed account (Dy7SLyTq)
change the && on line 144 to an ||
D'oh, just found where my problem was:

lines 57/58:
this->CharStrength=this->CharStrength+=2;
this->CharDefense=this->CharDefense+=1;

I originally had these as simply this->(property)+=1, (should that work out of interest, would save me some typing if yes) and switched when that didn't work. I forgot to take the = out of +=(x). I've just tested it again and it seems to be working as intended (well barring a space I forgot to cater for- "You levelled up to level2").

Line 144 is correct as far as I can tell, I only want the while loop to keep going when both conditions are true and exit as soon as one of them fails.
Thanks for the help anyway.
Last edited on
Topic archived. No new replies allowed.