values from functions
Dec 24, 2012 at 2:30am UTC
So in this code how do I get the the program to recognize that I add 10 to the HP? Because right now it just keeps going with fight function after the list function is over without adding the HP if the player chose to use the potion.
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
class combat{
public :
void fight(int ChHP){
srand(time(0));
int attack;
int smash;
int defend;
int hit1;
int hit2;
int EnHP=20;
int Chpower=5;
int Enpower=5;
int potions=1;
while (EnHP>0 && ChHP>0){
List2(potions, ChHP);
hit1=Chpower+rand()%5;
EnHP=EnHP-hit1;
cout << "you hit " << hit1 << endl;
cout << "The Enemys HP is " << EnHP << endl;
getchar();
if (EnHP<=0){
break ;
}
cout << endl;
hit2=Enpower+rand()%5;
ChHP=ChHP-hit2;
cout << "\nThe enemy hit you for " << hit2 << endl;
cout << "Your HP is " << ChHP << endl;
getchar();
};
if (ChHP<=0){
cout << "You Lose sorry, try agian!" << endl;
}
if (EnHP<=0){
cout << "YOU WIN" << endl;
}
}
int List2(int potions, int ChHP){
int x;
cout << "Press acording number " << endl;
cout << "1--Attack" << endl;
cout << "2--use potions, you have " << potions << " left" << endl;
cin >> x;
if (x==1){
}else {
if (potions>0){
potions=potions-1;
ChHP=ChHP+10;
cout << "Your HP is " << ChHP << endl;
}else {
cout << "You do not have enough potions!\n" ;
};
};
};
};
Dec 24, 2012 at 2:50am UTC
You need to pass ChHP by reference.
Line 39:
int List2(int potions, int & ChHP)
Dec 24, 2012 at 3:09am UTC
Thank you very much :)
Topic archived. No new replies allowed.