The Game Lobby program revisited

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
};

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

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()
{
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;
}
Last edited on
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
};

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();
string m_name;
string m_dname;
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_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;
}

else
{
Player* pTemp = m_pSearch ->GetNext();
m_pSecondary ->SetNext(pTemp);
delete m_pSearch;
found = true;
m_pSearch = 0;
m_pSecondary = 0;
}

}
else // this is the else statement giving me an error
{
m_pSecondary = m_pSearch;
m_pSearch = m_pSearch ->GetNext();
}



}
if (found == false)
{
cout << "Sorry, not found.\n\n";
}

}

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;
}
closed account (3qX21hU5)
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#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();
    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.

Nice challenge huh. :)
Last edited on
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#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();
    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)
		{
			return true;
		}
		pCheckPlayer = pCheckPlayer -> GetNext();
	}
	return false;
}

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;
				          }
				      }

			           else if(pNewPlayer ->GetName() > pIter2 ->GetName() && pNewPlayer -> GetName() < pIter -> GetName())
			              {

				             pIter2 ->SetNext(pNewPlayer);
				             pNewPlayer ->SetNext(pIter);
				             placed = true;
				             break;
			              }
			           else if(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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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) ;
}

Thanks a bunch cire. That worked perfect. Now if I could just combine the alphabetizing and the remove specific player functions together.
Topic archived. No new replies allowed.