Unkown problem with simple RPG

I'm fairly new to C++, in a sense that I know and understand a basic amount of knowledge. My problem, however, is implementing it, as I always seem to do something wrong. This basic program, I designed to give a player some stats, generate a random monster, and go through a fight phase where attacker 1's attack sum with a random number will have attacker 2's defense subtracted from it, and it will equal attacker 2's new HP level.

My problem is that once the program runs, the black window doesn't display a thing besides the blinking cursor.

The code is as follows (Sorry if I mess something up on my post, I'm new here.)

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
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>

using namespace std;


//Here are the General stats to be used by both the player and the opposing monster.
class Stats{
public:
    int attack;
    int defense;
    int health;
};



int main()
{
    srand(time(0));
    //The player's predetermined stats are declared.
    Stats player;
    player.attack=10;
    player.defense=10;
    player.health=50;
    //This loop exists simply to make the program run constantly.
    //The output won't be pretty, but I only expect it to follow through with the math.
    for(int i=0; i==0; i=i+0)
        {
            //Monster's stats are randomly generated.
            Stats monster;
            monster.attack=(rand()%20)+1;
            monster.defense=(rand()%20)+1;
            monster.health=(rand()%100)+1;
            //As long as neither the monster nor the player's health drops below zero, the combat will continue.
            while(monster.health<=0 || player.health<=0)
                {
                    //Damage taken by monster=(player's attack+random number)-monster's defense level
                    int playerdamage=(player.attack+((rand()%5)+1))-monster.defense;
                    monster.health=monster.health-playerdamage;
                    cout << "you attacked the monster for " << playerdamage << " damage. He is now down to " << monster.health << " hp!" << endl;
                    //vice versa; they take turns.
                    int monsterdamage=(monster.attack+((rand()%5)+1))-player.defense;
                    player.health=player.health-monsterdamage;
                    cout << "You were attacked for " << monsterdamage << " damage. You now have " << player.health << " hp." << endl;
                }
                //when someone's health drops to zero or below:
                if(monster.health<=0)
                    {
                        cout << "you killed the monster!" << endl;
                    }
                else if(player.health<=0)
                    {
                        cout << "you are dead." << endl;
                    }
        }
        //and it should repeat, filling the screen with text, as I don't know how to "pause" the console, but that's a different topic.

return 0;
}
Time to do a bit of debugging! Try to print some random stuff before, during, and after your loop. If you can print anything just before the loop, but it won't print anything in your loop, that means your loop is wrong.
Just a heads up, you can do an "infinite loop" a lot simpler than on line 29.

1
2
3
while(true)

for(;;)


Caveat: while(true) or while(1) will compare the expression every iteration to see if true is equal to true.

Your actual problem is on line 37.
Caveat: while(true) or while(1) will compare the expression every iteration to see if true is equal to true.


Only if you disable all possible optimizations (and even then, your compiler might do it anyway because it is so simple).
@firedraco
The more you know!
The condition on line 37 is wrong.

while ( monster.health >= 0 && player.health >= 0 )

would match the comment describing when the loop is supposed to execute. Currently the loop only executes if at least one of monster and player have a health that is negative.
Topic archived. No new replies allowed.