2013-05-09 58 views
0

即時通訊從房間從文本文件,並嘗試使用變量中的信息,所以我後我把信息添加到一個linkedList,搜索房間,我想要的房間ID,然後使用setRoomName =到它找到名稱的房間。但這不是工作...任何人都可以看到這個問題?setRoomName不改變變量roomName

這裏是我的代碼:

#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(); 
} 

任何幫助將不勝感激。

回答

0

我發現的錯誤是你找到一個房間通過ID,然後你是設置ID,名稱,退出爲你找到的相同。所以你怎麼能找到一個變化

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

取而代之的是,你應該給你需要爲Exmple

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

這裏發現房間的ID,名稱設置其他值,退出套到789, 「房間」,「是」分別爲