2013-01-10 68 views
0

我正在嘗試編寫一個簡單的遊戲大廳程序(沒有實際的網絡,只是一個模擬),而且我在測試程序時遇到運行時錯誤,我不知道如何去關於修理它。 (我對編程相當陌生)有關創建一個新對象的運行時錯誤

我的錯誤是當我去一個新的玩家節點進入大廳時,程序會接受我輸入的前兩個玩家名稱,但在第三次嘗試時我輸入了一個新的名稱,然後按回車鍵,但光標會變爲新行,而不是進入新節點。我懷疑這個問題在Lobby :: Add函數中,但我不確定它在哪裏。任何幫助或想法,將不勝感激。謝謝=)。

#include <iostream> 
#include <string> 

using namespace std; 

class Player 
{ 
public: 

    Player(const string& name = ""); 
    string GetName() const; 
    Player* GetNext() const; 
    void SetNext(Player* next); 

private: 
    string m_Name; 
    Player* m_pNext; 
}; 

Player::Player(const string& name): 
    m_Name(name), 
    m_pNext(0) 
{} 

string Player::GetName() const 
{ 
    return m_Name; 
} 

Player* Player::GetNext() const 
{ 
    return m_pNext; 
} 

void Player::SetNext(Player* next) 
{ 
    m_pNext = next; 
} 

class Lobby 
{ 
    friend ostream& operator<<(ostream& os, const Lobby& aLobby); 

public: 
    Lobby(); 
    ~Lobby(); 
    void Add(); 
    void Remove(); 
    void Clear(); 

private: 
    Player* m_pHead; 
}; 

Lobby::Lobby(): 
m_pHead(0) 
{} 

Lobby::~Lobby() 
{ 
    Clear(); 
} 

void Lobby::Add() 
{ 
    // Create a new player node 
    cout << "Please enter the name of new player: "; 
    string name; 
    cin >> name; 
    Player* pNewPlayer = new Player(name); 

    //If list is empty make head of list this new player 
    if (m_pHead == 0) 
    { 
     m_pHead = pNewPlayer; 
    } 

    else 
    { 
     Player* pIter = m_pHead; 

     while(pIter->GetNext() != 0) 
     { 
      pIter->GetNext(); 
     } 

     pIter->SetNext(pNewPlayer); 
    } 
} 

void Lobby::Remove() 
{ 
    if(m_pHead == 0) 
    { 
     cout << "The game lobby is empty, there are no players to remove!\n\n"; 
    } 

    else 
    { 
     Player* pTemp = m_pHead; 
     m_pHead = m_pHead->GetNext(); 
     delete pTemp; 
    } 
} 

void Lobby::Clear() 
{ 
    while(m_pHead != 0) 
    { 
     Remove(); 
    } 
} 

ostream& operator<<(ostream& os, const Lobby& aLobby) 
{ 
    Player* pIter = aLobby.m_pHead; 

    cout << "Here's who is in the game lobby: \n"; 

    if (pIter == 0) 
    { 
     cout << "The lobby is empty.\n"; 
    } 

    else 
    { 
     while(pIter != 0) 
     { 
      os << pIter->GetName() << endl; 
      pIter = pIter->GetNext(); 
     } 
    } 

    return os; 
} 

int main() 
{ 
    Lobby myLobby; 
    int choice; 

    do 
    { 
     cout << myLobby; 
     cout << "\nWelcome to the game lobby!\n"; 
     cout << "Please enter a choice.\n"; 
     cout << "0 - Quit the program.\n"; 
     cout << "1 - Add a player to the lobby.\n"; 
     cout << "2 - Remove a player from the lobby.\n"; 
     cout << "3 - Clear the lobby.\n\n"; 

     cout << "Choice: "; 
     cin >> choice; 

     switch(choice) 
     { 
      case 0: cout << "Goodbye!"; break; 
      case 1: myLobby.Add(); break; 
      case 2: myLobby.Remove(); break; 
      case 3: myLobby.Clear(); break; 
      default: cout << "Please enter a valid choice.\n"; break; 
     } 

    }while(choice != 0); 

    return 0; 
} 
+2

這是一個非常多的代碼。你能構建一個更簡單的例子來證明同樣的問題嗎? –

+0

曾聽說過一個調試器?另外,當提示用戶輸入某些內容時,你必須刷新輸出。 – 2013-01-10 17:39:10

+1

你有沒有考慮過使用'std :: list'或'std :: deque'來代替實現自己的鏈表? –

回答

1

添加時有一個無限循環。您致電GetNext(),但忘記將結果分配給pIter

我傾向於同意別人的看法,但您需要了解如何使用調試器。它還有助於在發佈之前將有問題的代碼減少到最小的一段代碼。在很多情況下,這已經可以讓你自己找到問題。在你的代碼中,你應該用std::cin替換爲固定值的輸入,這樣人們就不必猜測他們需要輸入什麼。

祝你好運!

Uli

+0

@Uli 謝謝,讓它工作。我正在努力獲得一些更好的硬件和更多的當前操作系統,希望我的調試器能夠正常工作。此外,感謝您提供建議,我會在發佈未來問題時記住它。 – Jammin