Thread: Writing a member variable to a text file

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    30

    Writing a member variable to a text file

    Hi,

    I have an EnemySprite object that has an int member variable called Chromosome. The value of it is "1".

    I'm trying to write that value to a text file, but instead of getting "1" I am getting "1244044", which I presume is the RAM address of the variable.

    I can't figure out why can't I get to the value of the variable.

    Here is a little code example:

    Code:
    void GA::StoreDeadEnemyInfo(Sprite* pEnemySprite) {
    
    ofstream f("Dead_Enemies.txt", ios::app);
       f << pEnemySprite->GetChromosome();
       f << "_";
       f << fitness;
       f << "\n";
    
    }
    Any advice on this is really appreciated. Thanks.

  2. #2
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    let's see prototype for GetChromosome()
    Last edited by Darryl; 11-08-2005 at 10:24 AM.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    30
    Code:
    class EnemySprite : public Sprite                                           
    {
      protected:
        int           m_iChromosome;                                
        int           m_iFitness;
      public:                                                                      
        ..........ETC................
        int GetChromosome() {
          return m_iChromosome;                                            
        };
    There's the key bits from the EnemySprite file.

  4. #4
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Well, I don't see anything wrong with it, perhaps it's not really storing a 1 as you think

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  2. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM