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.)
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>
usingnamespace 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;
}
elseif(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.
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.