Thread: Class array for easy loading of 3400 mechs

  1. #1
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373

    Class array for easy loading of 3400 mechs

    Im making a database of mechs for my game series MOTW.

    Heres what i have:
    Code:
    class Mech
    {
     private:
             string name;
             string weapon;
             string armor;
             string first_seen;
             string info;
             int hp;
             int def;
             int dam;
             int lvl;
     public:
             void setinfo(char * f);
             void outinfo();
    };
    
    void Mech::setinfo(char * f)
    {
     ifstream fin(f);
     getline(fin, name, '*');
     getline(fin, weapon, '*');
     getline(fin, armor, '*');
     getline(fin, first_seen, '*');
     getline(fin, info, '*');
     fin >> hp >> def >> dam >> lvl;
    }
    
    void Mech::outinfo()
    {
     cout << "Name: " << name << endl;
     cout << "Armor: " << armor << "\nDef: " << def << endl;
     cout << "Weapon: " << weapon << "\nMax Dam: " << dam << endl;
     cout << "Avg LVL: " << lvl << "\nFirst seen in: " << first_seen << endl;
     cout << "Desc:\n";
     cout << info << endl;
    }
    
    
    int main(int argc, char *argv[])
    {
      Mech mechs[3];
      for(int i = 0; i < 3; i++)
      {
       char * file;
       sprintf(file,"%d.mch", i);
       mechs[i].setinfo(file);
      }
      for (int i = 0; i < 3; i++)
      {
       mechs[i].outinfo();
       getche();
      }
    }
    This crashes when i try it. The entire database will have more than 5000 objects, and i dont wanna do Mech mech1; mech1.setinfo(1-3400.mch); etc

    Can i do that in a way that i am trying to do it? or do i have to do manual loading of each one?
    This war, like the next war, is a war to end war.

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    That's because you are using an uninitialized pointer here:

    Code:
    char * file;
    sprintf(file,"%d.mch", i);
    mechs[i].setinfo(file);
    You need to either just make an array like char file[256] or allocate memory for that pointer and make sure to free it somewhere.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373
    so something like:
    Code:
    for (int i = 0; i < 3; i++)
    {
     char  file[256];
     sprintf(file, "%d.mch", i);
     mechs[i].setinfo(file);
     delete file;
    }
    I don't do much pointer handling outside of *
    This war, like the next war, is a war to end war.

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Code:
    char  file[256];
    delete file; //bad!
    Never "delete" arrays. If you declare it as char file[256], it will automatically deallocate. IF it's char* file = new char[256], then you go delete[] file;

    Have fun
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  5. #5
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373
    I got it. Thanks guys.
    This war, like the next war, is a war to end war.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 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
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Loading an array using pointers
    By cheesehead in forum C++ Programming
    Replies: 5
    Last Post: 12-10-2001, 05:23 AM
  5. Replies: 4
    Last Post: 09-12-2001, 02:05 PM