0
\$\begingroup\$

I have a problem regarding how the values under a certain parameter doesn't change after being called again.

I have this kind of code, where, an imp (monster) and a character has to go for a battle:

#include <iostream>
using namespace std;

int gems = 0;

float imp (float imphp) {
float implife = imphp - 5.0;
return implife;
};

float character (float charlife) {
float charalives = charlife - 2.0;
return charalives;
};

int main () {
string charactername;
cout << "Hero Life: ";
cout << character(10.0) <<endl;
cout << "Monster Life: ";
cout << imp (10.0) <<endl;

};

My problem or rather a question is if I call the monster function again is that it returns the value as 1.

Code I used to call monster for the second time after the first turns of damage is done:

cout << monster << endl;

what happens there was it sends me a value of 1. how can i make it show the current value after the damage has been taken...? say if the imp takes another damage, how can i make my program show the current value, since if the imp took out 2.0 at first attack, then takes another 2.0, the value for the hp of the character to show is 6.0? Same with the other function, how can i return it to the current value?

\$\endgroup\$
1
  • 2
    \$\begingroup\$ You should try to reduce your problem to its simplest form when asking a question. You didn't need to bring "monsters" and "damage" and all that just to ask a question about how function local variables work. :) \$\endgroup\$
    – Paul Manta
    Commented Aug 5, 2012 at 5:38

1 Answer 1

0
\$\begingroup\$

The problem with these functions are that they are pretty much depending on what is going in.

Your functions are not objects as you intended them to be.

#include <iostream>
using namespace std;

int gems = 0;

float characterHealth  = 10.0f;    //Can work with till it goes out of scope
float enemyHealth = 10.0f;    //Can work with till it goes out of scope

float imp (float imphp) {
float implife = imphp - 5.0f;
return implife;
};

float character (float charlife) {
float charalives = charlife - 2.0f;
return charalives;
};

int main () {
string charactername;
cout << "Hero Life: ";
cout << character(characterHealth ) <<endl;
cout << "Monster Life: ";
cout << imp (enemyHealth) <<endl;

};

Your problem is that you work with local variables of a function that are completely depended on what is going in.

You were aiming a bit more for this ( I think):

#include <iostream>
using namespace std;

int gems = 0;

class Entity {
    public:
      Entity(float startingHealth) : health(startingHealth){}; // initialize health 
      float getHealth(){return health;}
      void setHealth(float value){ health = value;}; 
    private:
      float health;
};


float subtractHealthFrom(Entity& ent, float damage) {
   ent.setHealth(ent.getHealth() - damage);
   return ent.getHealth();
};

int main () {

 Entity character(10.0f);
 Entity enemy(10.0f);

 cout << "Hero Life: ";
 cout << subtractHealthFrom(character, 2.0f) <<endl;
 cout << "Monster Life: ";
 cout << subtractHealthFrom(enemy, 2.0f) <<endl;
 cout << "Hero Life: ";
 cout << subtractHealthFrom(character, 2.0f) <<endl;
 cout << "Monster Life: ";
 cout << subtractHealthFrom(enemy, 2.0f) <<endl;

};

This is just an example not THE way to do it. Just showing how you could manage data by creating classes and producing objects of them.

Also use f to tell the compiler that you are working with floats.

 float A = 1.0; // converts static double on right to float ( double is twice a float, therefor a double doesnt fit in a float)
 float B = 1.0f;// Nothing to do here, ok
\$\endgroup\$
2
  • \$\begingroup\$ if you are satisfied with my answer please accept my answer. It makes sure that others can see that you found your answer. \$\endgroup\$
    – Sidar
    Commented Aug 5, 2012 at 5:59
  • \$\begingroup\$ just voted for it, thanks a lot. now i can work with these examples you just gave. \$\endgroup\$
    – Angel
    Commented Aug 5, 2012 at 7:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .