Hello everyone. I am teaching myself about C++ programming and having a blast doing it. Not to long ago I bought a book, C++ through game programming 3rd edition. Reading through the book I got to the Game Lobby program. After trying it I decided to challenge myself. I want to be able to delete a specific player, by name, not first in first out. Any help with this is appreciated. I am a newbie with C++ so I have no idea where to start. Maybe I should not have challenged myself huh. Here is the program code:
#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
};
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;
}
//otherwise find the end of the list and add the player there
else
{
Player* pIter = m_pHead;
while (pIter->GetNext() != 0)
{
pIter = pIter->GetNext();
}
pIter->SetNext(pNewPlayer);
}
}
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;
}
}
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);
First of all, I suggest putting your code between the code tags [ code ] [ /code ] to preserve indentation and make it easier to read.
In the main function, typing "2" to remove a player calls the RemovePlayer() function:
1 2 3 4 5 6 7 8 9 10 11 12 13
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;
}
}
What you would need to do is to add a prompt inside the "else block" to ask for the player name. Then you use pTemp->GetName() to compare each player's name to the inputted name.
You can check the code for the AddPlayer() function to figure out how to go through all the players. Remember to re-set the pointers for the next player after you remove a player.
Hmm, ok. Well, as I said in the beginning I am learning C++ by self taught so I don't really think I know what you are referring to. Maybe showing me some code on how to get the ball rolling would be great. Any help would be greatly appreciated.
I have figured out how to place things so I may remove one specific player at a time but I am getting an error on a else statement. I looked over the code various times and cannot seem to find what it is talking about. Anyone got some suggestions??
#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
};
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;
}
//otherwise find the end of the list and add the player there
else
{
Player* pIter = m_pHead;
while (pIter->GetNext() != 0)
{
pIter = pIter->GetNext();
}
pIter->SetNext(pNewPlayer);
}
}
void Lobby::RemovePlayer()
{
cout << "Please enter the player you wish to remove: ";
cin >> m_dname;
m_pSearch = m_pHead;
m_pSecondary = m_pHead;
bool found = false;
while (m_pSearch != 0)
{
if (m_pSearch ->GetName() == m_dname);
{
if (m_pSearch == m_pHead)
{
m_pHead = m_pSearch ->GetNext();
delete m_pSearch;
}
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);
Once again please use the code tags under the format box (Hint they are the <> button off to the right when replying) and just paste your code between the [code] [ /code] boxes.
Also can you provide the error message and line number please?
Sorry. Guess I should of read how to post something. Ok, I figured out why I was getting an error. I had a semicolon where there should not have been one. Now, after read into this book more, I thought about alphabetizing the names I entered into the list. I know I would add another function, but the book don't really cover much more than that. Here is the code I have so far.
#include <iostream>
#include <string>
usingnamespace 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();
void AddPlayer();
void RemovePlayer();
void Alphabetize();
void Clear();
string m_name;
string m_dplayer;
Player* m_pSearch;
Player* m_pSecondary;
private:
Player* m_pHead;
};
Lobby::Lobby():
m_pHead(0)
{}
Lobby::~Lobby()
{
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;
}
//otherwise find the end of the list and add the player there
else
{
Player* pIter = m_pHead;
while (pIter->GetNext() != 0)
{
pIter = pIter->GetNext();
}
pIter->SetNext(pNewPlayer);
}
}
void Lobby::RemovePlayer()
{
cout << "Please enter the player you wish to remove: ";
cin >> m_dplayer;
m_pSearch = m_pHead;
m_pSecondary = m_pHead;
bool found = false;
while ( m_pSearch != 0 )
{
if ( m_pSearch ->GetName() == m_dplayer )
{
if (m_pSearch == m_pHead)
{
m_pHead = m_pSearch ->GetNext();
delete m_pSearch;
}
else
{
Player* pTemp = m_pSearch ->GetNext();
m_pSecondary ->SetNext(pTemp);
delete m_pSearch;
found = true;
m_pSearch = 0;
m_pSecondary = 0;
}
}
else
{
m_pSecondary = m_pSearch;
m_pSearch = m_pSearch ->GetNext();
}
}
if (found == false)
{
cout << "Sorry, " << m_dplayer << " not found.\n\n";
}
}
void Lobby::Alphabetize() //Here is where I get confused
{
}
void Lobby::Clear()
{
while (m_pHead != 0)
{
Player* pTemp = m_pHead;
m_pHead = m_pHead->GetNext();
delete pTemp;
}
}
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 - Alphabetize the list of names. \n";
cout << "4 - 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.Alphabetize(); break;
case 4: myLobby.Clear(); break;
default: cout << "That was not a valid choice.\n";
}
}
while (choice != 0);
system("pause");
return 0;
}
As you can see, I got the remove player to work. Thanks so much with that one. Now to alphabetize them. As you can also see, they will be alphabetized only when I call upon the function, not as I enter them into the list.
I got it. Took some time to figure out all my errors but I got it to alphabetize my list as I enter the names and to even tell me if I already have the name in the list. So, my question now is: Can anyone think of a better way to do this without so much coding.
#include <iostream>
#include <string>
usingnamespace 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();
void AddPlayer();
void RemovePlayer();
void Clear();
bool CheckPlayer(string name); //Added the new function
private:
Player* m_pHead;
};
Lobby::Lobby():
m_pHead(0)
{}
Lobby::~Lobby()
{
Clear();
}
bool Lobby::CheckPlayer(string name) //Here is the function code
{
Player* pCheckPlayer = m_pHead;
while (pCheckPlayer != 0)
{
if (pCheckPlayer -> GetName() == name)
{
returntrue;
}
pCheckPlayer = pCheckPlayer -> GetNext();
}
returnfalse;
}
void Lobby::AddPlayer()
{
cout << "Please enter the name of the new player: ";
string name;
cin >> name;
int moved = 0; //added
bool placed = false; //added
bool foundPlayer = false; //added
Player* pNewPlayer = new Player(name);
foundPlayer = CheckPlayer(name); //Everything from here to
if (foundPlayer)
{
cout << "\nPlayer exists in the list." << endl << endl;
}
else
{
if (m_pHead == 0)
{
m_pHead = pNewPlayer;
}
else
{
Player* pIter = m_pHead;
Player* pIter2 = pIter;
while(pIter != 0 && placed == false)
{
if(m_pHead ->GetNext() == 0)
{
if(pIter ->GetName() <= pNewPlayer ->GetName())
{
pIter ->SetNext(pNewPlayer);
placed = true;
break;
}
else
{
pNewPlayer ->SetNext(pIter);
m_pHead = pNewPlayer;
placed = true;
break;
}
}
elseif(pNewPlayer ->GetName() > pIter2 ->GetName() && pNewPlayer -> GetName() < pIter -> GetName())
{
pIter2 ->SetNext(pNewPlayer);
pNewPlayer ->SetNext(pIter);
placed = true;
break;
}
elseif(pNewPlayer ->GetName() <= m_pHead ->GetName())
{
pNewPlayer ->SetNext(m_pHead);
m_pHead = pNewPlayer;
placed = true;
break;
}
if(moved == 0)
{
pIter = pIter ->GetNext();
++moved;
}
else
{
if(pIter != 0)
{
pIter = pIter ->GetNext();
pIter2 = pIter2 ->GetNext();
}
}
if (placed == false)
{
pIter2 ->SetNext(pNewPlayer); //here is the new addition to alphabetize the list of names as they are entered.
}
}
}
}
}
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)
{
Player* pTemp = m_pHead;
m_pHead = m_pHead ->GetNext();
delete pTemp;
}
}
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;
}
Oh, and as you can see I could not implement the Remove Player from the previous code to work with this one. Had to start all over with original code. If anyone can do both, please let me know.
void Lobby::AddPlayer()
{
cout << "Please enter the name of the new player: ";
string name;
cin >> name;
if (CheckPlayer(name))
{
cout << "\nPlayer exists in the list.\n\n" ;
return ;
}
// Only create the new player when we're certain the name isn't already in the list.
Player* newPlayer = new Player(name) ;
// handle empty list
if (m_pHead == 0)
{
m_pHead = newPlayer;
return ;
}
// handle adding at head:
if ( newPlayer->GetName() < m_pHead->GetName() )
{
newPlayer->SetNext(m_pHead) ;
m_pHead = newPlayer ;
return ;
}
Player* previous = m_pHead ;
Player* current = previous->GetNext() ;
while(current && newPlayer->GetName() > current->GetName())
{
previous = current ;
current = current->GetNext() ;
}
previous->SetNext(newPlayer) ;
newPlayer->SetNext(current) ;
}