I am attempting to write a program that simulates a lobby or a line in which a "player" can get into in the form of a heap. The program does everything it is supposed to except when you try to add a second object("player") to the heap it won't do it.

I have spent all afternoon and evening trying to find an example of what I am attempting to do and I have not found anything that was much help. Assistance is needed and appreciated.

//Game Lobby
//Simulates a game lobby where players wait

#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;  //Pointer to next player in list
};

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 class constructor prototype
    ~Lobby();                  //destructor prototype
    
    void AddPlayer();          //AddPlayer member func. proto              
    void RemovePlayer();       //RemovePlayer member func. 
    void Clear();              //Clear member func.
    
private:
    Player* m_pHead;          //Pointer to first player in lobby
    Player* m_pTail;          //Pointer to copy of last player
};

Lobby::Lobby():         //Lobby class constructor definition
    m_pHead(0),
    m_pTail(0)
{}

Lobby::~Lobby()          //destructor definition
{
    Clear();
}

void Lobby::AddPlayer()
{
    //create a new player node
    cout << "Please enter the name of the 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;
        m_pTail = m_pHead->GetNext();
    }
    //otherwise find the end of the list and add the player there
    else
    {
         m_pTail = pNewPlayer;
         m_pTail = m_pTail->GetNext();                   
                                        
    }
}

void Lobby::RemovePlayer()
{
    if (m_pHead == 0)
    {
        cout << "The game lobby is empty.  No one to remove!\n";
    }
    else
    {
        Player* pTemp = m_pHead;
        m_pHead = m_pHead->GetNext();
        delete pTemp;
    }
}

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

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

    os << "\nHere's who's in the game lobby:\n";
    if (pIter == 0)
    {
        os << "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 << "\nGAME LOBBY\n";
        cout << "0 - Exit 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";
        cout << endl << "Enter choice: ";
        cin >> choice;

        switch (choice)
        {
            case 0: cout << "Good-bye.\n"; break;
	        case 1: myLobby.AddPlayer(); break;  
            case 2: myLobby.RemovePlayer(); break;
            case 3: myLobby.Clear(); break;
            default: cout << "That was not a valid choice.\n";
        }
	}
    while (choice != 0);
    
    system("PAUSE");
    return 0;
}

Thank you in advance.

Try this. GetNext() simply returns NULL and that is not what you want.

void Lobby::AddPlayer()
{
    //create a new player node
    cout << "Please enter the name of the 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;
        m_pTail = m_pHead; //m_pHead->GetNext();
    }
    //otherwise find the end of the list and add the player there
    else
    {
        m_pTail->SetNext(pNewPlayer);
         m_pTail = pNewPlayer;
         //m_pTail = m_pTail->GetNext();                   
                                        
    }
}

Perfect. I have been trying to figure why my prof. said that this needed another instance variable in the Player class for two days. I couldn't figure why but it has a head pointer, a tail pointer, it does everything it is supposed to and I think I know why. You were the only person on two sites that actually responded the problem I posted. Thank you.

This question has already been answered. Start a new discussion instead.