Cant seem to add to my heap.

My "lobby" class will only add one "player" to my

Page 1 of 1

2 Replies - 872 Views - Last Post: 26 July 2009 - 09:23 PM Rate Topic: -----

#1 dub4theworld  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 03-July 09

Cant seem to add to my heap.

Post icon  Posted 26 July 2009 - 09:00 PM

Hello, I am attempting to write a program that simulates a lobby or a line in the form of a heap. A "player" enters the lobby and if they are the first then they are considered the head of the line and assigned the "head" pointer and become the first object in the heap. Each subsequent player that enters the lobby gets in line and becomes the next object in the heap and is assigned the "tail" pointer. The purpose of the tail is to show the next player to get in line.

For a reason that I can't determine, I am only able to add one player to the heap. When I try to add another, nothing happens. I can't seem to find anything that provides an example of what I am trying to accomplish and I am stumped. Any and all suggestions are 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. prototype			  
	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;
}



Thanks in advance for your help.

Is This A Good Question/Topic? 0
  • +

Replies To: Cant seem to add to my heap.

#2 UG Cyber  Icon User is offline

  • D.I.C Addict

Reputation: 32
  • View blog
  • Posts: 556
  • Joined: 24-July 08

Re: Cant seem to add to my heap.

Posted 26 July 2009 - 09:11 PM

You should use vectors for this. It can save a lot of effort. You can use a vector to store the names on the fly. Make the vector a global so you can access it in all functions with out passing it as a param. It should make things alot easier.
Was This Post Helpful? 0
  • +
  • -

#3 dub4theworld  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 03-July 09

Re: Cant seem to add to my heap.

Posted 26 July 2009 - 09:23 PM

View PostUG Cyber, on 26 Jul, 2009 - 08:11 PM, said:

You should use vectors for this. It can save a lot of effort. You can use a vector to store the names on the fly. Make the vector a global so you can access it in all functions with out passing it as a param. It should make things alot easier.




The assignment requires the use of a heap. If not I would have been done 5 hours ago. Thanks for the advise though.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1