Hi, I'm having trouble reading a file the way I want it to.

/*Textfile.txt*/
ROOM
Closet
Its too dark, you cant see anything.
EXIT
out Basement 1
ROOM
Basement
Its too dark, you need some sort of light to help you see.
EXIT
enter Closet 1
EXIT
up Dinning_Room 1
ROOM
Dinning_Room
It looks like no one is here. Everything looks old and dusty.
EXIT
down Basement 1


/*Function: Reads the file
 *Parameter: string for file name, vector of Rooms
 *Return: void
 */
void openFile(std::string fileName, std::vector<Room*> *room){
    Room* _room;
    std::string Name;
    std::string Desc;
    std::string Dir;
    std::string check;
    std::string check2;
    bool exit;
    std::ifstream file(fileName);

    if (file.is_open())
    {
        while (file.good()){
            std::getline(file, check);
            if (check == "ROOM")
            {
                std::getline(file, Name);
                std::getline(file, Desc);
                _room = new Room(Desc, Name);
                file >> check2;
                if (check2 == "EXIT") {
                    file >> Dir >> Name >> exit;
                    _room->SetExit(Dir, Name, exit);
/*stores information from SetExit() in a room vector in Room Object, can have multiple "Exits"*/
                    std::cout << "Registered exits for " << _room->getName() << ": " << Name << std::endl;
                    std::getline(file, check2);
                    std::cout << check2;
                }
                room->push_back(_room);
            }
        }
        file.close();
    }
    else
    {
        std::cout << "no file";
    }
}

whats happening is that the Basement to Dining_Room is not registering.

Recommended Answers

All 3 Replies

I don't see a Basement to Dining_Room transition. There is a Basement to Closet transition, and a Dining_Room to Basement transition.

I fixed this with a better file format, now indicating the number of exit and used it in a for-loop. Inside the for-loop, I created the exit and push_back it in the vector.

I hope my answer helped you find this solution. In any case, I'm happy you have solved the problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.