Thread: Char array problem.

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    33

    Char array problem.

    Hi guys,

    Having a problem returning a whole char array from a function. (Player name)

    For example, I have a player class, one of the attributes is char playerName[10].

    I have a public function getName() to return this array. It currently only returns the first letter of the name.

    This is the function to return array. (as defined in a corresponding .cpp file )
    Code:
    char Player::getFirstName()
    {
         
         return *playerFirstName;
         
         
         }

    Any ideas on what i'm doing wrong?

    Regards,

    -Kirill

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Maybe you want to return a char * instead of a single char?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    33
    Quote Originally Posted by matsp View Post
    Maybe you want to return a char * instead of a single char?

    --
    Mats


    Tried this, now i'm getting 0..

    Code:
    char* Player::getFirstName()
    {
         
         return playerFirstName;
         
         
         }
    -Kirill

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You probably need to post a bit more code. What calls getFirstName, and where is playerFirstName declared, and where is it set?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Aug 2008
    Posts
    33
    Quote Originally Posted by matsp View Post
    You probably need to post a bit more code. What calls getFirstName, and where is playerFirstName declared, and where is it set?

    --
    Mats
    Okay, there are three files in my project, Player.h (Player class, where playerFirstName) Player.cpp (definitions of functions from Player.h) and maingame.cpp (main driver)

    maingame.cpp call the Player constructor, and also calls the getFistName() function.

    -Kirill

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What I meant is that we probably need to see the SOURCE where those things happen [or even better, a stripped down version of your code that compiles with the relevant bits that cause you problems present in the code as a single source file].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Aug 2008
    Posts
    33
    Quote Originally Posted by matsp View Post
    What I meant is that we probably need to see the SOURCE where those things happen [or even better, a stripped down version of your code that compiles with the relevant bits that cause you problems present in the code as a single source file].

    --
    Mats
    Ah okay, i'll post up my whole project, because I haven't got that much code at the moment anyways:

    This is game.cpp (the driver, and where everything will happen

    Code:
    #include <iostream>
    #include "Player.h"
    
    using namespace std;
    
    
    int main()
    {
        
        char first[10];
        char last[10];
        
        
        cout<< "Please enter first name: " << endl;
        cin>> first;
        cout<< "\nPlease enter last name: " << endl;
        cin>> last;
        
        
        Player player1(first, last);
        
        
        //This is just a test of the Player functions, notice they return only the first letter of array.
        cout<< "Player 1 first name: " << player1.getFirstName() << endl;
        cout<< "Player 1 last name: " << player1.getLastName() << endl;
        
        
        
        system("PAUSE");
        
    }

    This is Player.h, defines the Player class and its' attributes:

    Code:
    #include <string.h>
    
    class Player
    {
          
          
          public:
                 Player(char *firstName, char *lastName);
                 
                 char* getFirstName();
                 char getLastName();
                 int getPosition();
                 void printFirstName();
                 void printLastName();
                 int getPlayerNumber();
                 enum skill{BATTER, PITCHER, CATCHER, FIELDER};
              
               
                 
                 
                 
          
          
          private:
          
                  char playerFirstName[10];
                  char playerLastName[10];
                  
                  
                  
                  bool teamId;
                  enum position{OFF_FIELD, PITCHER_PLATE, BATTING_PLATE, 
                                FIRST_BASE, SECOND_BASE, THIRD_BASE, 
                                 INNER_RIGHT, INNER_CENTER, INNER_LEFT,
                                  OUTER_RIGHT_FOUL, OUTER_RIGHT_WALL,
                                  OUTER_LEFT_FOUL, OUTER_LEFT_WALL,
                                  HOME_RUN};
                                  
                  
                                
                  skill currentSkill;                
                  int playerNumber;
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          };
    Finally, this is Player.cpp, defines all the functions in Player.h :

    Code:
    #include "Player.h"
    
    using namespace std;
    
    
    
    Player::Player(char *firstName, char *lastName)
    {
                        
              
                        
                  strcpy(playerFirstName, firstName);
                  strcpy(playerLastName, lastName);      
                        
                        
                        }
                        
              
    
                        
                        
    char* Player::getFirstName()
    {
         
         return playerFirstName;
         
         
         }    
         
    char Player::getLastName()
    {
         
         return *playerLastName;
         
         }       
         
    int Player::getPlayerNumber()
    {   
        
        return playerNumber;
        
    }
    Phew! sorry about such a monstrous post!

    Any idea on what i'm doing wrong?

    Regards,

    -Kirill

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Are you sure your comment is completely correct. In the getLastName (which you haven't updated to return an array) I would expect so. In the printout of getFirstName I would expect to see the full name.

    [I just merged all your code into one file, changed the getLastName as I suggested above, and using g++ to compile it, it does what I expect].

    You do have an awful lot of blank lines in your code. I'm not saying you shouldn't have blank lines, just that having more than 3 is usually not helpful to the reader.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Aug 2008
    Posts
    33
    Quote Originally Posted by matsp View Post
    Are you sure your comment is completely correct. In the getLastName (which you haven't updated to return an array) I would expect so. In the printout of getFirstName I would expect to see the full name.

    [I just merged all your code into one file, changed the getLastName as I suggested above, and using g++ to compile it, it does what I expect].

    You do have an awful lot of blank lines in your code. I'm not saying you shouldn't have blank lines, just that having more than 3 is usually not helpful to the reader.

    --
    Mats
    Absolutely, what i quoted here is EXACTLY my code. It still doesn't work, now i've updated the getLastName() function, so now i'm getting returns of 0 for first name and nothing for last name!



    -Kirill

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, I just built and tested your code [aside from me doing the job of the preprocessor and making it into one file]. This works:
    Code:
    #include <iostream>
    #include <string.h>
    
    using namespace std;
    
    class Player
    {
    public:
        Player(char *firstName, char *lastName);
                 
        char* getFirstName();
        char* getLastName();
        int getPosition();
        void printFirstName();
        void printLastName();
        int getPlayerNumber();
        enum skill{BATTER, PITCHER, CATCHER, FIELDER};
          
    private:
          
        char playerFirstName[10];
        char playerLastName[10];
    
        bool teamId;
        enum position{OFF_FIELD, PITCHER_PLATE, BATTING_PLATE, 
    		  FIRST_BASE, SECOND_BASE, THIRD_BASE, 
    		  INNER_RIGHT, INNER_CENTER, INNER_LEFT,
    		  OUTER_RIGHT_FOUL, OUTER_RIGHT_WALL,
    		  OUTER_LEFT_FOUL, OUTER_LEFT_WALL,
    		  HOME_RUN};
                                
        skill currentSkill;                
        int playerNumber;
    };
    
    
    Player::Player(char *firstName, char *lastName)
    {
        strcpy(playerFirstName, firstName);
        strcpy(playerLastName, lastName);      
    }
                       
    char* Player::getFirstName()
    {
        return playerFirstName;
    }    
         
    char *Player::getLastName()
    {
        return playerLastName;
    }       
         
    int Player::getPlayerNumber()
    {   
        return playerNumber;
    }
    
    int main()
    {
        
        char first[10];
        char last[10];
        
        
        cout<< "Please enter first name: " << endl;
        cin>> first;
        cout<< "\nPlease enter last name: " << endl;
        cin>> last;
        
        
        Player player1(first, last);
        
        
        //This is just a test of the Player functions, notice they return only the first letter of array.
        cout<< "Player 1 first name: " << player1.getFirstName() << endl;
        cout<< "Player 1 last name: " << player1.getLastName() << endl;
        
        system("PAUSE");
        
    }
    Aside from blank lines, formatting and merging to one file, I only changed Player::getLastName().

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Aug 2008
    Posts
    33
    This is strange.

    Could the fact it's in separate files be the problem? But then again, it does see the function, and does return something.. just not the whole array.. i'm super confused. Btw, i'm using Dev-cpp as my IDE.

    Regards,

    -Kirill

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by kbro3 View Post
    This is strange.

    Could the fact it's in separate files be the problem? But then again, it does see the function, and does return something.. just not the whole array.. i'm super confused. Btw, i'm using Dev-cpp as my IDE.
    Which means that you are indeed using the same g++ 3.4.x that I'm using [ok, so I think I've got 3.4.5, and yours may be 3.4.2 - but that's a few obscure bug fixes in difference, nothing major].

    I'm also convinced that the separate files have nothing do to with it.

    Are you sure that you changed BOTH the header and the source when you changed the function?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by kbro3 View Post
    Any ideas on what i'm doing wrong?
    Using char arrays instead of std::string.

  14. #14
    Registered User
    Join Date
    Aug 2008
    Posts
    33
    Quote Originally Posted by medievalelks View Post
    Using char arrays instead of std::string.
    I'm required by assignment guidelines to use char arrays...

    -Kirill

  15. #15
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    why don't you try passing in a char * to the getter, assign that to the member variable containing the string, and have getfirstname return the length of the string?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. problem working with char array
    By bradleym83 in forum C Programming
    Replies: 4
    Last Post: 07-27-2005, 10:57 AM
  3. Problem Putting INTs Into a CHAR Array
    By cram in forum C++ Programming
    Replies: 13
    Last Post: 10-13-2004, 07:53 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM