struct fighter {
int hp;
string name;
}
fighter1, fighter2;
int attack (int hp)
{
int damage = rand ()% 5+1;
cout <<"the hit did"; cout << damage; cout << "damage\n\n";
int new_hp;
new_hp=hp-damage;
return (new_hp);
}
int main () {
srand ((unsigned) time (NULL));
if (fighter1.hp<=0 && fighter2.hp<=0)
cout << "its a tie";
else if (fighter1.hp<=0)
cout << "hanz won the match\n";
else if (fighter2.hp<=0)
cout << "gonzo won the match\n ";
else
cout << "error\n";
}
getch ();
return 0;
}
as you can see there is no way to see who deals damage to who
Please use code tags to preserve indentation (use the # button to the right of the edit box and put your code between the tags it generates).
You are nowhere displaying the names of the fighters. Presumably you want to do that in attack, in which case you may as well pass the whole fighter structure instead of just the hp member.
Your while loop is only controlling the first line after it due to lack of braces. You probably mean this:
1 2 3 4 5
while (fighter1.hp>0 && fighter2.hp>0)
{
fighter1.hp = attack(fighter1.hp); // I haven't modified these lines, but you should.
fighter2.hp = attack(fighter2.hp);
}
Actually, it would make more sense to have a member function called attack.