Game assignement trouble updating life

arme()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int Personnage::arme(Personnage *pCible)
{

	srand(time(NULL));

	if( this->bArme() == true)
	{
		int iRand = rand() % (this->m_ptArme->getiDegatMax()) + (this->m_ptArme->getiDegatMin());

		pCible->m_iVie -= iRand;

		std::cout << pCible->getiVie() << std::endl;

		return iRand;
	}

	else return -1;
	
}

1
2
3
4
5

cout	<< "Votre attaque a fait " << ((iTurn % 2) ? p1->arme(p2) : p2->arme(p1))
	<< " degat, il reste donc a " << ((iTurn % 2) ? p2->getsName() : p1->getsName())
	<< " : " <<  ((iTurn % 2) ? p2->getiVie() : p1->getiVie())
	<< " point de vie" << endl;


I have this code and everytime I'm trying to get p2->getiVie() it always returning me 80 wich is the starting number of life of this p2

I guess you will need more things just let me know because I've got alot of classes

also when I get the life of the personnage in the method arme() it's returning me the right number also if I use the getiVie() later on the code it's working

so I have not clue neeed some help please
I know I bump and I should not to but this work is to be given tomorow so I really need your help guy
Well, I'm not sure if I can explain it correctly but at least I know how to correct it.

You just have to assign the results of your function to variables before displaying them with cout.
Cout is probably displaying the result of your function as it is when called. So the damage weren't substracted to your character yet.

For example :

1
2
3
int degats = ((iTurn % 2) ? p1->arme(p2) : p2->arme(p1));
int vieRestante = ((iTurn % 2) ? p2->getiVie() : p1->getiVie());
cout << "Votre attaque a fait " << degats << " degat, il reste donc a " << ((iTurn % 2) ? p2->getsName() : p1->getsName()) << " : " << vieRestante << "point de vie"" << endl; 
Last edited on
I've corrected this problem by outputing the result in the function who,s in the class personnage so it get direct acces to the atribute


Mais merci pour ton aide je l'ai arranger d'une autre facon xD mais merci
Topic archived. No new replies allowed.