HI i want to exit this loop whenif(comphealth <= 0 || yourhealth <= 0) that if statement is true.how do i do that.like can i put something after the if statement?
You want the loop to end when one of the healths have depleted. Therefore, make the while condition so that both need to be above zero in order for it to execute.
Or not. Global variables are best avoided. They may seem pretty harmless in smaller programs but can wreak havoc in larger scale ones. I would recommend looking at function return types and function parameters (both pass-by-value and pass-by-reference).
You re define new varibles in every function. The varibles you have only run the life of the function or block of code it runs. You declare health 4 times, which creates 4 SEPERATE, completely different varibles, becuase they were created in function, so they will run only through the life of the function. in c++ you can have 400 different varibles with the same name in different function, it might be complete hell to find errors, but c++ allows it. Try decalaring your varibles one in main, and passing them into functions like so:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
void Playerfight(int& x, int& y, int dmg);
int main()
{
int health;
int comphealth;
int damage;
int compdamage;
char * name;
Playerfight(health,comphealth,damage)
}
void Playerfight(int& x, int& y, int dmg)
{
std::cout << "Your current health: " << x;
std::cout << "Enemies Current health: " << y;
y -= dmg;
std::cout << "You did " << dmg << " Damage! Enemy now has " << y << " health left!\n";
}