fighting simulator

i have been working on a fighting program but i have a little problem

when i start up the program i cant see wich fighter is hitting who here is the code so far

#include <iostream>
#include <string>
#include <time.h>
#include <sstream>
#include <conio.h>
using namespace std;


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));


fighter1;
fighter2;
fighter1.hp=50;
fighter2.hp=50;
fighter1.name="gonzo";
fighter2.name="hanz";

{
while (fighter1.hp>0 && fighter2.hp>0)

fighter1.hp = attack(fighter1.hp);
fighter2.hp = attack(fighter2.hp);

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.
you could pass the fighters name to attack such as:

fighter1.hp = attack(fighter1.hp,fighter1.name, fighter2.name);
fighter2.hp = attack(fighter2.hp,fighter2.name, fighter1.name);
then for int attack()

int attack (int hp, string attacher,defend)

now you can display who is attacking who and for how much damage
thanks for the help


but i have noticed another problem only fighter 2 aka hanz keeps winning i think its because fighter 1 does'nt hit back any ideas why?
Do you have the braces, as below?

1
2
3
4
5
while (fighter1.hp>0 && fighter2.hp>0)
{
    fighter1.hp = attack(fighter1.hp);
    fighter2.hp = attack(fighter2.hp);
}

I think its because fighter 1 does'nt hit back any ideas why?

Maybe he's a pacifist? ... Sorry it had to be done, your post made me lol
Ali vs Gandhi !
Topic archived. No new replies allowed.