Confusing problem with file input

This is a discussion on Confusing problem with file input within the C++ Programming forums, part of the General Programming Boards category; I'm trying to load from a temporary hand made data file that I'm using for testing. I recently reworked this ...

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

    Confusing problem with file input

    I'm trying to load from a temporary hand made data file that I'm using for testing. I recently reworked this code and suddenly it doesn't work. I marked a position in the code which I've found through extensive testing. When the for loop has looped five times, the program suddenly STOPS. No error message, no crash, it simply STOPS. I have no idea why and I'm yanking my hair out trying to get this to work.

    charID and roomID are arrays I'm using to store pointers to objects of the Character and Room classes. Both classes are stripped down to accessors and mutators due to problems I've had, so they can't be the source of the problem. Can anyone see what's causing my very odd problem?

    Here's the piece of code. I bolded the line that it dies on.
    Code:
        int id, intInput, numRooms, numItems;
        string input;
    
        cout << "Loading realm... ";
        ifstream io(WORLD_FILE);
    
        charID[0] = new Character;
    
        io >> numRooms;        //Number of Rooms
        io >> ROOM_ITEM_LIMIT; //Max items per room
        io >> intInput;        //Starting Location
        charID[0]->setLocation(intInput); //Set player position
        io.ignore();
    
        //Load Rooms
        Room* newRoom;
        for(int i = 0; i < numRooms; i++){
            io >> id;                   //Room's ID
            roomID[id] = new Room; //!!!*IT DIES HERE*!!!
            io.ignore();
    
            getline(io, input);         //Room's Name
            roomID[id]->setName(input);
            getline(io, input);         //Room's Description
            roomID[id]->setDesc(input);
    
            for(int dir = 0; dir < 12; dir++){ //Room's Exits
                io >> input;
                roomID[id]->setDirExit(dir, parseInt(input));
            }
    
            for(int i = 0; i < ROOM_ITEM_LIMIT; i++){ //Room's Items
                io >> input;
                roomID[id]->setItem(i, parseInt(input));
            }
    
            int count;
            io >> count;
            io.ignore();
            roomID[id]->setItemCount(count);
        }
    Here's part of the data file I'm using. I've only included the data up to the point that it dies. I bolded the last piece of data the program loads before dieing.

    12 5 3
    0
    Outpost
    You are in the armory corner of the outpost. Scattered weapons and armor line the wall. The supply has obviously run short.
    -1 -1 1 -1 3 -1 -1 -1 -1 -1 -1 -1
    1 2 -1 -1 -1
    2
    1
    Outpost
    The tall watch tower stands in this otherwise empty corner. A barely stable ladder leads to the tower's observation post.
    -1 -1 -1 -1 2 -1 0 -1 4 -1 -1 -1
    -1 -1 -1 -1 -1
    0
    2
    Outpost
    The commander's office is settled in an out of the way section of the outpost. Directly across is a small barracks, which also serves as the dining hall.
    1 -1 -1 -1 -1 -1 3 -1 -1 -1 5 -1
    -1 -1 -1 -1 -1
    0
    3
    Outpost
    The gate of the outpost is guarded by a sentry in tattered leather armor leans against his spear, half asleep.
    0 -1 2 -1 -1 -1 -1 -1 -1 -1 -1 -1
    3 -1 -1 -1 -1
    1
    4
    Watch Tower
    You can see far into the wilderness from here. The bored watchman is sharpening an arrowhead while leaning against a support beam.
    -1 -1 -1 -1 -1 -1 -1 -1 -1 1 -1 -1
    -1 -1 -1 -1 -1
    0

  2. #2
    Registered User dizolve's Avatar
    Join Date
    Dec 2002
    Posts
    68
    Do some error checking. Like, if (numRooms <= 0) { numRooms = 1; } and print some debug messages out. I can't see anything wrong but of course I don't have all of your code.

    &nbsp;&nbsp;__&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;___&nbsp;& nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;&nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;/\&nbsp;\&nbsp;&nbsp;__&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;/\_&nbsp;\&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;\_\&nbsp;\/\_\&nbsp;&nbsp;____&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_ __\//\&nbsp;\&nbsp;&nbsp;&nbsp;&nbsp;__&nbsp;&nbsp;__&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;__&nbsp;&nbsp;&nbsp;
    &nbsp;/'_`&nbsp;\/\&nbsp;\/\_&nbsp;,`\&nbsp;&nbsp;/&nbsp;__`\\&nbsp;\&nbsp;\&nbsp;&nbsp;/\&nbsp;\/\&nbsp;\&nbsp;&nbsp;/'__`\&nbsp;
    /\&nbsp;\_\&nbsp;\&nbsp;\&nbsp;\/_/&nbsp;&nbsp;/_/\&nbsp;\_\&nbsp;\\_\&nbsp;\_\&nbsp;\&nbsp;\_/&nbsp;|/\&nbsp;&nbsp;__/&nbsp;
    \&nbsp;\___,_\&nbsp;\_\/\____\&nbsp;\____//\____\\&nbsp;\___/&nbsp;\&nbsp;\____\
    &nbsp;\/__,_&nbsp;/\/_/\/____/\/___/&nbsp;\/____/&nbsp;\/__/&nbsp;&nbsp;&nbsp;\/____/
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;I&nbsp;have&nbsp;a&nbsp;BAD&nbsp;figlet& nbsp;addiction.

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    As near as I can tell the first int in your text file is read into numRooms, that number is 12, yet there are only 5 rooms listed in your file.

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    42
    That's only a part of the file. I felt there wasn't a need to show any further into it (where it has data on the remaining rooms, items, etc) because I knew where the error was (lots and lots of variable checking). There is actually a lot more to it.

    -edit-
    I fixed the problem by just rewriting the darned thing (it's not like it was all that big anyway). I must have had something wrong before, because everything works fine now.
    Last edited by Vorok; 01-05-2003 at 03:36 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  2. Problem with Creating File for File Processing
    By Dampecram in forum C Programming
    Replies: 2
    Last Post: 12-07-2008, 12:26 AM
  3. file input output problem..
    By epidemic in forum C++ Programming
    Replies: 10
    Last Post: 12-03-2006, 02:55 AM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. File input problem
    By robert_sun in forum C Programming
    Replies: 1
    Last Post: 05-14-2004, 05:54 AM

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