Thread: Totally puzzling IO problem

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    42

    Totally puzzling IO problem

    This is really, really odd. For a game I'm working on, I'm trying to load the attributes of rooms, items, etc. from a file. I get a very, very strange problem during my loading process.

    I've done some testing and it appears that once it reads in a certain number of characters, everything goes wrong.

    I get this from my debug code: (ç is actually capitalized, but I can't find a capital version of it)

    ID: 2
    NAME: buckler
    DESC: This round, iron shield is dented and rusted.
    ID: 3
    NAME: gate
    DESC: The gate is çS= oor condition. It looks hastily built.
    ID: 4
    *CRASHES HERE*

    Here's the part of my loading code that crashes. Before this, the program loads the information for the rooms in the game. The odd part is that if I comment out the bolded line below, it works FINE. I'm dumbfounded, because all that line does is set the description of the item! I also underlined the line that causes the error.

    Before the loop, I declared "string input" and "int id". itemID is a temporary local array of Item pointers that I use to load my items in (so I can hang onto them while putting them in rooms).

    Code:
    //LOAD ITEMS
        for(int i = 0; i < numItems; i++){
            io >> id;
            cout << "ID: " << id << endl; //DEBUG
            io.ignore();
            getline(io, input);
            cout << "NAME: " << input << endl; //DEBUG
            itemID[i]->setName(input);
            getline(io, input);
            cout << "DESC: " << input << endl; //DEBUG
            itemID[i]->setDesc(input);
            io >> intInput;
            itemID[i]->setGetable(intInput);
            io >> intInput;
            itemID[i]->setUsable(intInput);
            io >> intInput;
            itemID[i]->setCharges(intInput);
            io >> intInput;
            itemID[i]->setDestroyOnDeplete(intInput);
            io >> intInput;
            itemID[i]->setOpenStatus(intInput);
            io >> intInput;
            itemID[i]->setGoDest1(intInput);
            io >> intInput;
            itemID[i]->setGoDest2(intInput);
            io >> intInput;
            itemID[i]->setEquippable(intInput);
            io >> intInput;
            itemID[i]->setDoor(intInput);
        }
    Here's the function that sets the description of the item.
    Code:
    void Item::setDesc(string newDesc)
    {
        desc = newDesc;
    }
    And finally, here's the half of the data file I'm loading from that the for loop sees. The top half of the file has the room data.

    5
    0
    letter
    A letter from General Isendol giving you command over the outpost. It is stamped with the royal military seal.
    1 0 0 0 -1 -1 -1 0 0
    1
    spear
    A basic wooden shaft with a blade for a head.
    1 0 0 0 -1 -1 -1 1 0
    2
    buckler
    This round, iron shield is dented and rusted.
    1 0 0 0 -1 -1 -1 1 0
    3
    gate
    The gate is in poor condition. It look hastily built.
    0 0 0 0 0 7 3 0 1
    4
    post
    A human skull is impaled on a spike. There is orcish lettering written on a dangling cloth.
    0 0 0 0 0 -1 -1 0 0 0

    If this stuff isn't easy to read, I can attach some source.

    Does anyone have any clue as to why I'm having this wierd problem? I've tried to fix it but can't.

    -edit-
    I misquoted the exact messages printed by my debug code. I fixed that.
    Last edited by Vorok; 01-06-2003 at 04:00 PM.

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    42
    I've done some extensive testing on my program since I posted the above problem and am now even more baffled. It will mess up even when I seperate the item data into its own file, load it in an entirely different file input stream, close the previous input stream, and use entirely different strings to load the information with. I don't get it. The only time the error won't occur is if I don't call setDesc().

    On another odd note, I'm using Dev-C++, and if I run the program in "debug mode", it properly loads my data. I also edited my previous post to show where the actual error occurs (I underlined it).
    Last edited by Vorok; 01-06-2003 at 04:01 PM.

  3. #3
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    Code:
    void Item::setDesc(string newDesc)
    {
        desc = newDesc;
    }
    You might want to do this instead
    Code:
    void Item::setDesc(const string& newDesc)
    {
        desc = newDesc;
    }
    I don't think this is the real problem though.
    Can you attach the source?

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    42
    Here's the source. I cleaned it up a bit to make it easier to understand. And as you figured, the change to my setDesc() function didn't solve the problem. For my future reference, why is "const string& newDesc" better?

  5. #5
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    I think I found the problem.
    Look at your Room.h There is
    an "empty array" items. To my knowledge this
    isnt allowed in c++ but gcc allows it as some sort
    of extension. It's strange because when I ran the code
    through the debugger it worked but without the debugger
    running it would crash.

    const string& avoids the copy. Also if string inherits from
    another class it avoid slicing the object.

    Also, old versions of gcc dont have namespaces but new ones do
    so this took a bit of work to get running.

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    42
    Thank you so much! I hadn't even considered that there was a problem like that in my Room class. When I was doing some rewrites I must have forgotten to put a number in there.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with file IO
    By Chunky in forum C++ Programming
    Replies: 20
    Last Post: 08-08-2006, 01:43 PM
  2. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  3. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  4. crashing problem in c++
    By Enki in forum C++ Programming
    Replies: 6
    Last Post: 11-08-2002, 01:21 AM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM