A class problem

This is a discussion on A class problem within the C++ Programming forums, part of the General Programming Boards category; First of all, hello! Im new to the forums. Im still kind of new to c++, and im working on ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    5

    A class problem

    First of all, hello! Im new to the forums. Im still kind of new to c++, and im working on understanding classes at the moment. Below is some code ive typed up that im having trouble with. Here is the code.

    Code:
    #include <iostream>
    
    std::string cClass;
    
    class Player{
          
          public:
                 
                 struct sPLAYER{
                        int hp;
                        int en;
                        int mana;
                        int gold;
                 };
                 
                 sPLAYER sP;
                 
                 void setName(std::string cName1){
                      cName2 = cName1;
                      }
                      
                 std::string getName(){
                             return cName2;
                             }
                 
                 
                 void setClass(int n){
                      if( n == 1 ){
                          cClass = "Knight";
                          }
                      else if( n == 2 ){
                           cClass = "Mage";
                           }
                      else if( n == 3 ){
                           cClass = "Thief";
                           }
                      else{
                           std::cout << "\n\n - Invalid Class Type -  ";
                           }
                 }
                 
                 std::string getClass(){
                             return cClass;
                             }
                             
    
                void setStats(std::string cClass){
                      if( cClass == "Knight" ){
                          sP.hp = 20;
                          sP.en = 20;
                          sP.mana = 13;
                          sP.gold = 100;
                          }
                      else if( cClass == "Mage" ){
                           sP.hp = 25;
                           sP.en = 14;
                           sP.mana = 25;
                           sP.gold = 100;
                           }
                      else if( cClass == "Thief" ){
                           sP.hp = 17;
                           sP.en = 23;
                           sP.mana = 13;
                           sP.gold = 115;
                           }
                      else{
                           std::cout << " Error - Invalid Class ";
                           }
                      }
                      
                 int getStats(){
                     std::cout << " HP: " << sP.hp << std::endl;
                     std::cout << " Energy: " << sP.en << std::endl;
                     std::cout << " Mana: " << sP.mana << std::endl;
                     std::cout << " Starting Gold: " << sP.gold << std::endl;
                     }
                       
                          
          private:
                  std::string cName2;
                  
    };
    
    Player Player;
    
    int main(){
        
        int n;
        std::string cName1;
        
        std::cout << "\n\n Enter Character Name: ";
        std::cin >> name1;
        Player.setName(name1);
        
        std::cout << "\n\n --- Select Character Class ---";
        std::cout << "\n\n 1. Knight";
        std::cout << "\n 2. Mage";
        std::cout << "\n 3. Theif\n ";
        std::cin >> n;
        Player.setClass(n);
        Player.setStats(cClass);
        std::cin.get();
        
        std::cout << "\n\n Player Information ";
        std::cout << "\n\n Name: " << Player.getName() << std::endl;
        std::cout << " Class: " << Player.getClass() << std::endl;
        std::cout << " Stats: " << Player.getStats() << std::endl;
        std::cin.get();
    }
    -- Sorry if its a lot ---
    Ok, so here's the problem. When I run the program, I enter a character name, and then I pick a class type. After I pick the class type it shows my characters name, class type and stats. But the problem is how it shows the information. Here is a demonstration.

    Code:
    Enter Character Name: Luminost
    
    
    --- Select Character Class ---
    1. Knight
    2. Mage
    3. Theif
    1
    
    
    Player Information
    
    Name: Luminost
    Class: Knight
    HP: 20
    Energy: 20
    Mana: 13
    Starting Gold: 100
    
    Stats: 4473824
    I cant figure out why it displays the class stats, then displays 'Stats:' with some random number. Can anyone enlighten me on the problem and how to fix it?

  2. #2
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    It's the return value of the function getStats(). The problem is that getStats prints itself the values, yet you call it from within a cout. So when you call getStats(), it will print the stats but then return and since the return type is an integer, it will return some random garbage.

    You should rethink your code. getStats() is not the same as printStats() logically, so either you write get functions that have a return value or you write a print function that takes care of the output itself. But you can't mix both.

  3. #3
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    This program doesn't compile. I suggest you look it over.

    getStats() doesn't return a value.

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    5
    Just before I read your posts, I had changed
    Code:
    std::cout << " Stats: " << Player.getStats() << std::endl;
    
    // to
    
    std::cout << "\n -Class Stats-" << std::endl;
    Player.getStats();
    And it works exactly how I wanted it too. So, is there some other more recommened way I should do it, or is this fine?

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    And it works exactly how I wanted it too. So, is there some other more recommened way I should do it, or is this fine?
    Better rename getStats to printStats, so the name reflects better what the function does and won't mislead you again.

    Also you'll need to change the return type to void if it doesn't return a value. Otherwise you leave yourself open to similar mistakes and the compiler would complain if you turned on its warnings.

  6. #6
    Registered User
    Join Date
    Mar 2007
    Posts
    5
    Ok, ill do that. How do you turn on the warnings. I figured mine were on.

  7. #7
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    Quote Originally Posted by Luminost
    Ok, ill do that. How do you turn on the warnings. I figured mine were on.
    Depends... what compiler/IDE are you using?

  8. #8
    Registered User
    Join Date
    Mar 2007
    Posts
    5
    Dev C++

  9. #9
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    Try, Tools > Compiler Options > Settings > Inhibit all warnings > No

  10. #10
    Registered User
    Join Date
    Mar 2007
    Posts
    5
    Mine already says no, does that mean they're on, or do I switch it to yes?

  11. #11
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You'll probably also need to add -Wall to Tools > Compiler Options > Compiler > add to compiler box. (Visit GCC reference pages to find about other compiler flags.)

    By default, you are not inhibiting them, but you aren't requesting for them either.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mesh Class Design problem
    By sarah22 in forum Game Programming
    Replies: 2
    Last Post: 05-20-2009, 04:52 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 08:10 AM
  4. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  5. Replies: 3
    Last Post: 12-03-2001, 12:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21