Thread: class trouble

  1. #1
    lv.42 Berserker Drake's Avatar
    Join Date
    Jun 2005
    Posts
    67

    Exclamation class trouble

    Hey, i'm finally picking up where I left last time in C++ after a full year of Java. Well I picked up at classes and decided that a great practice would be to make a little battle simulator. In the following code I have 2 classes (Enemy and Player), all the code compiles but my two functions attack(Enemy enemyN, Player playerN) and attack(Player playerN, Enemy enemyN) don't seem to work.

    1. Enemy ernie's health does not decrease when I call the attack function.


    WARNING: THIS CODE WILL GET MESSY!!! I cannot find my old compiler that worked like a charm, instead im stuck with this crap bloodshed that dosn't indent for crap!

    Code:
    #include <stdio.h>
    #include<iostream>
    #include<string>
    
    using namespace std;
    string input;
    int pClass;//players class
    
    
    class Player
    {
          public:
    
          int exp;//Current experience points
          int hp;//Max health points
          int mp;//Max magic points
          int def;//Max Defense
          int str;//Max Strenght
          string name;//Player's name
    
          void stats()
          {
               cout<<".~-Stats-~."<<endl;
               cout<<name<<endl;
               cout<<"HP: "<<hp<<endl;
               cout<<"MP: "<<mp<<endl;
               cout<<"Defense: "<<def<<endl;
               cout<<"Strength: "<<str<<endl;
               cout<<"Exp: "<<exp<<endl;
               system("pause");
        }
    
        void bStats()
        {
               cout<<name<<endl;
               cout<<"HP: "<<hp<<endl;
               cout<<"MP: "<<mp<<endl;
               cout<<"\n\n\n"<<endl;
        }
    
    };
    
    class Enemy
    {
          public:
    
          int hp;//Max health points
          int mp;//Max magic points
          int def;//Max Defense
          int str;//Max Strenght
          string name;
          void stats()
          {
               cout<<name<<endl;
               cout<<"HP: "<<hp<<endl;
               cout<<"MP: "<<mp<<endl;
               cout<<"Defense: "<<def<<endl;
               cout<<"Strength: "<<str<<endl;
               system("pause");
          }
          void bStats()
          {
               cout<<name<<endl;
               cout<<"HP: "<<hp<<endl;
               cout<<"MP: "<<mp<<endl;
               cout<<"\n\n\n"<<endl;
          }
    
    };
          int attack(Enemy enemyN, Player playerN)
          {
               enemyN.hp = enemyN.hp - playerN.str;
               return enemyN.hp;
          }
          int attack(Player playerN, Enemy enemyN)
          {
               playerN.hp = playerN.hp - enemyN.str;
               return playerN.hp;
          }
    
          void tBattle(Player p, Enemy e)
          {
               e.stats();
               cout<<"\n\n\n"<<endl;
               p.stats();
               cout<<"\n\n";
               system("pause");
               system("cls");
               cout<<"Press 'a' to attack..."<<endl;
               cin>>input;
               if(input == "a")
               {
                        attack(e, p);
                        system("cls");
                        e.bStats();
                        cout<<"\n\n\n"<<endl;
                        p.bStats();
                        cout<<"\n\n";
                        system("pause");
                        system("cls");
               }
    
          }
    
    
          int main()
          {
               Enemy ernie;
               ernie.name = "Ernie";
               ernie.hp = 50;
               ernie.mp = 20;
               ernie.def = 10;
               ernie.str = 3;
               Player p1;
               cout<<"     Welcome to Arena v1.0      "<<endl;
               system("pause");
               system("cls");
               cout<<"What is your Name?"<<endl;
               cout<<"Name: ";
               cin>>p1.name;
               system("cls");
               cout<<"Please choose one of the following classes... "<<endl;
               cout<<"1. Warrior\n2. Mage\n3. Ranger\n"<<endl;
               cout<<"class: ";
               cin>>pClass;
    
               switch(pClass)
               {
                        case 1:
    
                        p1.hp = 150;
                        p1.mp = 30;
                          p1.def = 10;
                        p1.str = 10;
                        p1.exp = 0;
                        cout<<"\n\nYou chose Warrior!\n"<<endl;
                        system("pause");
                        system("cls");
                        p1.stats();
                        break;
    
                        case 2:
    
                        p1.hp = 80;
                        p1.mp = 120;
                        p1.def = 4;
                        p1.str = 4;
                        p1.exp = 0;
                        cout<<"\n\nYou chose Mage!\n"<<endl;
                        system("pause");
                        system("cls");
                        p1.stats();
                        break;
    
                        case 3:
    
                        p1.hp = 120;
                        p1.mp = 50;
                        p1.def = 6;
                        p1.str = 6;
                        p1.exp = 0;
                        cout<<"\n\nYou chose Ranger!\n"<<endl;
                        system("pause");
                        system("cls");
                        p1.stats();
                        break;
    
                        default:
    
                        cout<<"invalid class\n";
               }
    
               system("cls");
               cout<<"(Tutorial)"<<endl;
               system("pause");
               system("cls");
               cout<<"Welcome young combatant to the Arena!..."<<endl;
               system("pause");
               cout<<"\nYou shall practice with the prisoner Ernie.  Ohh, don't worry my dear lad! Poor Ernie here hasn't been fed in 2 weeks!\n"<<endl;
               system("pause");
               system("cls");
    
               tBattle(p1, ernie);
    
               return 0;
       
          }

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    WARNING: THIS CODE WILL GET MESSY!!! I cannot find my old compiler that worked like a charm, instead im stuck with this crap bloodshed that dosn't indent for crap!
    Bloodshed allows for users to change the indentation options. Is that... crap too?
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Try passing by reference to change a a value in a called function.

    Freebie beautification:
    Code:
    #include <stdio.h>
    #include<iostream>
    #include<string>
    
    using namespace std;
    string input;
    int pClass;//players class
    
    
    class Player
    {
    public:
    
       int exp;//Current experience points
       int hp;//Max health points
       int mp;//Max magic points
       int def;//Max Defense
       int str;//Max Strenght
       string name;//Player's name
    
       void stats()
       {
          cout<<".~-Stats-~."<<endl;
          cout<<name<<endl;
          cout<<"HP: "<<hp<<endl;
          cout<<"MP: "<<mp<<endl;
          cout<<"Defense: "<<def<<endl;
          cout<<"Strength: "<<str<<endl;
          cout<<"Exp: "<<exp<<endl;
          system("pause");
       }
    
       void bStats()
       {
          cout<<name<<endl;
          cout<<"HP: "<<hp<<endl;
          cout<<"MP: "<<mp<<endl;
          cout<<"\n\n\n"<<endl;
       }
    
    };
    
    class Enemy
    {
    public:
    
       int hp;//Max health points
       int mp;//Max magic points
       int def;//Max Defense
       int str;//Max Strenght
       string name;
       void stats()
       {
          cout<<name<<endl;
          cout<<"HP: "<<hp<<endl;
          cout<<"MP: "<<mp<<endl;
          cout<<"Defense: "<<def<<endl;
          cout<<"Strength: "<<str<<endl;
          system("pause");
       }
       void bStats()
       {
          cout<<name<<endl;
          cout<<"HP: "<<hp<<endl;
          cout<<"MP: "<<mp<<endl;
          cout<<"\n\n\n"<<endl;
       }
    
    };
    int attack(Enemy enemyN, Player playerN)
    {
       enemyN.hp = enemyN.hp - playerN.str;
       return enemyN.hp;
    }
    int attack(Player playerN, Enemy enemyN)
    {
       playerN.hp = playerN.hp - enemyN.str;
       return playerN.hp;
    }
    
    void tBattle(Player p, Enemy e)
    {
       e.stats();
       cout<<"\n\n\n"<<endl;
       p.stats();
       cout<<"\n\n";
       system("pause");
       system("cls");
       cout<<"Press 'a' to attack..."<<endl;
       cin>>input;
       if ( input == "a" )
       {
          attack(e, p);
          system("cls");
          e.bStats();
          cout<<"\n\n\n"<<endl;
          p.bStats();
          cout<<"\n\n";
          system("pause");
          system("cls");
       }
    
    }
    
    
    int main()
    {
       Enemy ernie;
       ernie.name = "Ernie";
       ernie.hp = 50;
       ernie.mp = 20;
       ernie.def = 10;
       ernie.str = 3;
       Player p1;
       cout<<"     Welcome to Arena v1.0      "<<endl;
       system("pause");
       system("cls");
       cout<<"What is your Name?"<<endl;
       cout<<"Name: ";
       cin>>p1.name;
       system("cls");
       cout<<"Please choose one of the following classes... "<<endl;
       cout<<"1. Warrior\n2. Mage\n3. Ranger\n"<<endl;
       cout<<"class: ";
       cin>>pClass;
    
       switch ( pClass )
       {
       case 1:
    
          p1.hp = 150;
          p1.mp = 30;
          p1.def = 10;
          p1.str = 10;
          p1.exp = 0;
          cout<<"\n\nYou chose Warrior!\n"<<endl;
          system("pause");
          system("cls");
          p1.stats();
          break;
    
       case 2:
    
          p1.hp = 80;
          p1.mp = 120;
          p1.def = 4;
          p1.str = 4;
          p1.exp = 0;
          cout<<"\n\nYou chose Mage!\n"<<endl;
          system("pause");
          system("cls");
          p1.stats();
          break;
    
       case 3:
    
          p1.hp = 120;
          p1.mp = 50;
          p1.def = 6;
          p1.str = 6;
          p1.exp = 0;
          cout<<"\n\nYou chose Ranger!\n"<<endl;
          system("pause");
          system("cls");
          p1.stats();
          break;
    
       default:
    
          cout<<"invalid class\n";
       }
    
       system("cls");
       cout<<"(Tutorial)"<<endl;
       system("pause");
       system("cls");
       cout<<"Welcome young combatant to the Arena!..."<<endl;
       system("pause");
       cout<<"\nYou shall practice with the prisoner Ernie.  "
             "Ohh, don't worry my dear lad! "
             "Poor Ernie here hasn't been fed in 2 weeks!\n"<<endl;
       system("pause");
       system("cls");
    
       tBattle(p1, ernie);
    
       return 0;
    
    }
    Consider finding your own beautifier.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    903
    It's generally better programming practice to separate a function declaration from its implementation.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by Desolation
    Enterprise!
    It's a toy program. He doesn't have to follow good practice to experiment.

  6. #6
    lv.42 Berserker Drake's Avatar
    Join Date
    Jun 2005
    Posts
    67

    Lightbulb

    thanks, I was reading ahead of where I am at in my book and I found that all I had to do was call the function with an object pointer. But, I could have also called it with a reference operator!!! Thanks from those who tried to help

  7. #7
    lv.42 Berserker Drake's Avatar
    Join Date
    Jun 2005
    Posts
    67
    Quote Originally Posted by Mario F.
    Bloodshed allows for users to change the indentation options. Is that... crap too?
    well, considering I had no idea you could do that. You could have put that in nicer words, im sure of that.

  8. #8
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Sure I could. But I decided to follow your lead.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  2. Replies: 3
    Last Post: 10-31-2005, 12:05 PM
  3. Trouble with a class variable
    By TravisDane in forum C++ Programming
    Replies: 9
    Last Post: 12-08-2002, 12:59 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM