c++ - Instance Variables Not Updating -


when run little game, battle commences after every turn, players attack health doesn't go down (i.e. game never ends , keeps printing same health on , over). know has inflictdamage system. can see error?

#include <iostream> #include <string> #include <iomanip> #include <cmath> #include <ctime> #include <cstdlib> #include <vector> #include <limits>  using namespace std;  class hulk { private:     string playername;     string color;     double weight; //in kilograms     double height; //in centimeters     double health;     int strength;     int speed;     vector<char> moveset; public:     hulk(const string& playername, string color):         playername(playername), color(color) {             this->weight = 500.0;             this->height = 200.0;             this->health = 110.0;             this->strength = 200;             this->speed = 50;             moveset.push_back('p');             moveset.push_back('k');             moveset.push_back('s');             if(playername == " ")             {                 cout << "not valid name!" << endl;                 exit(1);             }     }     string getplayername()     {         return this->playername;     }     double getweight()     {         return this->weight;     }     double getheight()     {         return this->height;     }     double gethealth()     {         return this->health;     }     void sethealth(double x)     {         this->health = x;     }     void takedamage(double x)     {         this->health -= x;     }     void inflictdamage(double x, hulk enemy)     {         enemy.takedamage(x);     }     void smash(hulk x)     {         this->inflictdamage(50.0, x);     }     void punch(hulk x)     {         this->inflictdamage(25.0, x);     }     void kick(hulk x)     {         this->inflictdamage(30.0, x);     }     vector<char> getmoveset()     {         return this->moveset;     } }; int main() {     /** creates random number. */     srand(static_cast<unsigned>(time(0)));      cout << "this hulk battle test." << endl;     vector<string> names(3);     names[0] = "randy";     names[1] = "chris";     names[2] = "carlos";     cout << "please enter name: ";     string playername;     cin >> playername;     hulk player(playername, "green");     int r = rand() % 3;     hulk enemy(names[r], "green");     cout << player.getplayername() << " vs. " << enemy.getplayername() << "!" << endl;     bool gameover = false;     while(gameover == false)     {         char playerturn;         bool playermove = false;         while(playermove == false)         {             cout << "your turn, do? ((p)unch, (k)ick, (s)mash): ";             cin >> playerturn;             if(playerturn == 'p')             {                 player.punch(enemy);                 playermove = true;             }             else if(playerturn == 'k')             {                 player.kick(enemy);                 playermove = true;             }             else if(playerturn == 's')             {                 player.smash(enemy);                 playermove = true;             }             else             {                 cout << "this not valid move. please try again." << endl;                 cin.clear();                 cin.ignore(numeric_limits<streamsize>::max(), '\n');             }         }         if(enemy.gethealth() <= 0.0)         {             cout << "you have defeated " << enemy.getplayername() << ". game over!" << endl;             gameover = true;         }         char enemyturn;         bool enemymove = false;         while(enemymove == false)         {             vector<char> enemymoves = enemy.getmoveset();             int movenumber = rand() % 3;             char move = enemymoves[movenumber];             if(move == 'p')             {                 enemy.punch(player);                 cout << "you have been punched , lost 25 health. @ " << player.gethealth() << " health." << endl;                 enemymove = true;             }             else if(move == 'k')             {                 enemy.kick(player);                 cout << "you have been kicked , lost 30 health. @ " << player.gethealth() << " health." << endl;                 enemymove = true;             }             else if(move == 's')             {                 enemy.smash(player);                 cout << "you have been smashed , lost 50 health. @ " << player.gethealth() << " health." << endl;                 enemymove = true;             }         }         if(player.gethealth() <= 0.0)         {             cout << enemy.getplayername() << " has defeated you! game over!" << endl;             gameover = true;         }     } } 

a method like

void inflictdamage(double x, hulk enemy) {     enemy.takedamage(x); } 

cannot change object enemy, since object passed "by value", means, every time call inflictdamage(x,e), there created copy of e, , takedamage applied copy, not original e.

what want is

void inflictdamage(double x, hulk &enemy) {     enemy.takedamage(x); } 

which makes enemy passed "by reference".


Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -