0

我从文本文件中获取房间并尝试使用变量中的信息,所以我在获取信息后将其添加到链接列表中,通过房间 ID 搜索我想要的房间,然后使用 setRoomName = 到房间它找到名称。但这不起作用...任何人都可以看到问题吗?

这是我的代码:

#include "Rooms.h"
#include "DoublyLinkedList.h"
#include <string>
using namespace std;

DoublyLinkedList<Rooms> roomsList;
DoublyLinkedListIterator<Rooms> itr = roomsList.getIterator();

 Rooms :: Rooms()
{
    this->roomID = 0;
    this->roomName = "";
    this->roomExits = "";
}

 Rooms :: Rooms(int roomID,string roomName,string roomExits)
 {
     this->roomID = roomID;
     this->roomName = roomName;
     this->roomExits = roomExits;
 }
    int Rooms :: getRoomID()
    {
        return roomID;
    }
    void Rooms :: setRoomID(int roomID)
    {
        this->roomID = roomID;
    }
    string Rooms :: getRoomName()
    {
    return roomName;
    }
    void Rooms ::setRoomName(string roomName)
    {
        this->roomName = roomName;
    }
    string Rooms :: getRoomExits()
    {
    return roomExits;
    }
    void Rooms :: setRoomExits(string roomExits)
    {
        this->roomExits = roomExits;
    }
 void Rooms :: loadRooms()
 {
    string fileName = "Rooms\\Rooms.txt";
    ifstream infile(fileName);
    string garbage;
    int loadRoomID;
    string loadRoomName1;
    string loadRoomName2;
    string loadRoomExits;
    while(infile >>garbage >> garbage >> loadRoomID  >> garbage >>
          garbage >> garbage >> loadRoomName1 >> loadRoomName2 >> garbage 
          >> garbage >> loadRoomExits)
    {

        cout << "Room ID: \t\t"<< loadRoomID << "\n";
        cout << "Room Name: \t\t"<< loadRoomName1 <<" " << loadRoomName2 << "\n";
        cout << "Room Exits: \t\t" << loadRoomExits <<"\n";
        string loadRoomName = loadRoomName1 + loadRoomName2;
        Rooms r1 (loadRoomID,loadRoomName,loadRoomExits);
        roomsList.Append(r1);
    }

 }
 void Rooms :: printRooms()
    {
        int index = 0; 
        //Loop through the iterator.
        for(itr.Start();itr.Valid();itr.Forth())
        {
            index++;
            cout << "------------------Rooms------------------\n";
            cout << "--------------------------------\n";
            cout << "Position:\t\t" << index << "\n";
            cout << "--------------------------------\n";
            cout << "Room ID:\t\t" << itr.Item().getRoomID() << "\n"; 
            cout << "Room Name:\t\t" << itr.Item().getRoomName() << "\n"; 
            cout << "Room Exits:\t\t" << itr.Item().getRoomExits() << "\n";
            cout << "------------------------------------------\n";
        }
        cout << "Rooms: \t\t" << roomsList.getCount() << "\n";
    }
    string Rooms :: searchByRoomID(int searchByID)
{
    //Loop through the Iterator.
    for (itr.Start(); itr.Valid(); itr.Forth())
        {
            //If the object entered has the same first name as the one in the loop.
            if (itr.Item().getRoomID() == searchByID)
            {
                //Print out the details from the list.
                cout << "------------------------------------------\n"; 
                cout << "Room ID:\t\t" << itr.Item().getRoomID() << "\n"; 
                cout << "Room Name:\t\t" << itr.Item().getRoomName() << "\n"; 
                cout << "Room Exits:\t\t" << itr.Item().getRoomExits() << "\n";
                cout << "------------------------------------------\n";
                setRoomID(itr.Item().getRoomID());
                setRoomName(itr.Item().getRoomName());
                setRoomExits(itr.Item().getRoomExits());
                cout << getRoomID();
                cout << getRoomName();
                cout << getRoomExits();
            }
        }
    return getRoomName();
}

任何帮助将不胜感激。

4

1 回答 1

0

我发现的错误是您正在通过 id 找到一个房间,然后您将 id、name、exits 设置为与您找到的相同。那么如何找到改变

setRoomID(itr.Item().getRoomID());
setRoomName(itr.Item().getRoomName());
setRoomExits(itr.Item().getRoomExits());

取而代之的是,您应该提供一些需要为 Exmple 设置的其他值

setRoomID(789); 
setRoomName("Room ");
setRoomExits("yes");

这里找到的房间的 id,name,exits 分别设置为 789,"Room","Yes"

于 2013-05-09T19:03:06.357 回答