c++ - Can't figure out what's wrong not a right variable value in class after functions and so on -


heyo! got interested in c++ coding out of blue , started researching, decided write little text based pokemon game! can't figure out wrong in code, maybe i'm doing newb mistake have no knowledge for? because don't know should search in google. here code:

class charmander { public: int lvl=0; int xp=0; int str=15+3*lvl; int agi=15+3*lvl; int hp=30+6*lvl; int energy=25+5*lvl; int cen=0; int nxp=1000; }cha;  class squirtle { public: int lvl=0; int xp=0; int str=15+3*lvl; int agi=15+3*lvl; int hp=30+6*lvl; int energy=25+5*lvl; int cen=0; int nxp=1000; }squ;  class pikachu { public: int lvl; int xp; int str=10+2*lvl; int agi=25+5*lvl; int hp=25+5*lvl; int energy=25+5*lvl; int cen=0; int nxp=1000; }pik;  class bulbasaur { public: int lvl=0; int xp=0; int str=20+4*lvl; int agi=10+2*lvl; int hp=35+7*lvl; int energy=25+5*lvl; int cen=0; int nxp=1000; }bul;  class currentpokemon { public: int lvl=0; int fl=0; int xp=0; int str=0; int agi=0; int hp=0; int energy=0; int cen=0; int nxp=1000; string tag; string name; }curr;  int level () {     int lvlup;     lvlup = curr.xp/curr.nxp;     while (lvlup >= 1) {         lvlup = curr.xp/curr.nxp;         if (lvlup >= 1) {         curr.lvl++;         curr.xp=curr.xp-curr.nxp;         curr.nxp=curr.nxp*1.5;         cout << "you reached level " << curr.lvl << " !" << endl;          }         else {             break;         }     }     }  int update () {     if (curr.lvl >> curr.fl) {         if (curr.name == "pikachu") {             pik.nxp=curr.nxp;             pik.xp=curr.xp;             pik.lvl=curr.lvl;             pik.cen=curr.cen;             curr.str=pik.str;             curr.agi=pik.agi;             curr.hp=pik.hp;             curr.energy=pik.energy;         }      } }   int main() {     string a,c1,c2,c3,c4,g;     int f;     cout << "you starting journey pokemon master!" << endl;     sleep (200);     cout << "you have choose companion advantures experience!" << endl;     sleep (200);     tryagain:     cout << "c charmander, s squirtle, p pikachu, b bulbasaur!" << endl;     cin >> c1;     if (c1 == "c") {         = "charmander";         cha.str = cha.str + 10;         cha.agi = cha.agi + 7;         cha.hp = cha.hp + 20;         cha.energy = cha.hp + 20;         cha.cen = cha.cen + 20;         curr.str = cha.str;         curr.agi = cha.agi;         curr.hp = cha.hp;         curr.energy = cha.energy;         curr.cen = cha.cen;         curr.tag = "fire";         curr.name = "charmander";     }     else if (c1 == "s") {         = "squirtle";         squ.str = squ.str + 12;         squ.agi = squ.agi + 10;         squ.hp = squ.hp + 25;         squ.energy = squ.energy + 20;         squ.cen = squ.cen + 20;         curr.str = squ.str;         curr.agi = squ.agi;         curr.hp = squ.hp;         curr.energy = squ.energy;         curr.cen = squ.cen;         curr.tag = "water";         curr.name = "squirtle";     }     else if (c1 == "p") {         = "pikachu";         pik.str = pik.str + 8;         pik.agi = pik.agi + 15;         pik.hp = pik.hp + 15;         pik.energy = pik.energy + 20;         pik.cen = pik.cen + 20;         curr.str = pik.str;         curr.agi = pik.agi;         curr.hp = pik.hp;         curr.energy = pik.energy;         curr.cen = pik.cen;         curr.tag = "eletricity";         curr.name = "pikachu";     }     else if (c1 == "b") {         = "bulbasaur";         bul.str = bul.str + 15;         bul.agi = bul.agi + 2;         bul.hp = bul.hp + 30;         bul.energy = bul.energy + 20;         bul.cen = bul.cen + 20;         curr.str = bul.str;         curr.agi = bul.agi;         curr.hp = bul.hp;         curr.energy = bul.energy;         curr.cen = bul.cen;         curr.tag = "plant";         curr.name = "bulbasaur";     }     else {         cout << "sorry, wrong choice, try again!" << endl;         goto tryagain;     }     cout << "you have chosen " << << " ,may friends forever!" << endl;     sleep (200);     tryagain2:     cout << "would pass training course pokemon? (y/n)" << endl;     cin >> c2;     if (c2 == "y") {         curr.xp = curr.xp + 1500;         curr.str = curr.str + 20;         curr.agi = curr.agi + 5;         curr.hp = curr.hp + 5;         curr.energy = curr.energy + 10;         curr.cen = curr.cen - 15;     }     else if (c2 == "n") {         cout << "very then." << endl;     }     level();     update();     cin >> f;     if (f==1) {         curr.xp = curr.xp + 600;     }     level();         if (f==2) {         curr.xp = curr.xp + 4600;     }     level();     update();     cout << " lvl " << curr.lvl << " xp " << curr.xp << " str " << curr.str << " agi " << curr.agi << " hp " << curr.hp << " energy " << curr.energy << " current energy" << curr.cen << " " << curr.tag << endl;     cout << " lvl " << pik.lvl << " xp " << pik.xp << " str " << pik.str << " agi " << pik.agi << " hp " << pik.hp << " energy " << pik.energy << " current energy" << pik.cen << " " << endl;     return 0; } 

only did int update () pikachu after every step want check if right. choices p -> y -> 2 want is: str 10 -> p -> str 18 -> y -> 2 -> (at time level 3) -> str 24 ,but str 18, agi 40, hp 40, energy 45, current energy 5, xp 1350, lvl 3 (on both lines, curr , pik).

if me out problem appreciate greatly! have nice day , thanks.

please consider alternate design. perhaps this:

class pokemon { protected:   std::string name;   int lvl;   int xp;   int str;   int agi;   int hp;   int energy;   int cen;   int nxp; public:   pokemon(std::string name);   int level ();   int update (); };  class charmander : public pokemon { public:   charmander (); };  charmander::charmander (std::string name)    : pokemon(name) {   name = name;   lvl=0;   xp=0;   str=15+3*lvl;   agi=15+3*lvl;   hp=30+6*lvl;   energy=25+5*lvl;   cen=0;   nxp=1000; }; ... 

factor common functionality base class "pokemon".

add per-class functionality subclasses (charmander, pikachu, etc).

you should make functions "level()" , "update()" part of base class. make them "virtual" functions if behavior different class class; example, if "pikachu.update()" different "charmander.update()".

book recommendation:

it's not short - is great introduction both c++ , object oriented design strategies using c++.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -