I dont really know where to start

I have bought beginning C++ through game programming and i am so stuck one one of the last practice exercises, ive been trying to figure it out for the past 2 days and i havent gotten anywhere, someone please help me get started, thanks. The question is:


Improve the Lobby class from the Game Lobby program by writing a friend function of the Player class that allows a Player object to be sent to cout. Next, update the function that allows a Lobby object to be sent to cout so that it uses your new function for sending a Player object to cout.

Here is the code:

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
//Game Lobby
//Simulates a game lobby where players wait

#include <iostream>
#include <string>

using namespace std;

class Player
{
public:
	Player(const string& name = " ");
	string Get_Name() const;
	Player* Get_Next() const;
	void Set_Next(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::Get_Name() const
{
	return m_Name;
}

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

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

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

public:
	Lobby();
	~Lobby();
	void Add_Player();
	void Remove_Player();
	void Clear();

private:
	Player* m_pHead;
};

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

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

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

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

	//otherwise
	else
	{
		Player* pIter = m_pHead;
		while ((*pIter).Get_Next() !=0)
		{
			pIter = (*pIter).Get_Next();
		}
		(*pIter).Set_Next(pNew_Player);
	}
}

void Lobby::Remove_Player()
{
	if (m_pHead==0)
	{
		cout<<"The game lobby is empty. There is no one to remove";
	}
	else
	{
		Player* pTemp = m_pHead;
		m_pHead = (*m_pHead).Get_Next();
		delete pTemp;
	}
}

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

ostream& operator<<(ostream& os, const Lobby& a_lobby)
{
	Player* pIter = a_lobby.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).Get_Name()<<endl;
			pIter = (*pIter).Get_Next();
		}
	}

	return os;
}


int main()
{
	Lobby my_lobby;
	int choice;

	do
	{
		cout<<"my_lobby";
		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 a choice: ";
		cin>>choice;

		switch (choice)
		{
			case 0: cout<<"Good-bye.\n";break;
			case 1: my_lobby.Add_Player(); break;
			case 2: my_lobby.Remove_Player(); break;
			case 3: my_lobby.Clear(); break;
			default: cout<<"That wasn't a valid choice.\n";
		}

	}while (choice!=0);

	return 0;
}
Last edited on
right you are learning, only with c++ it doesnt feel like it, this is what i have noticed...i itried reading your code but started to fall asleep...it might be easier to read if you re-post with thecode between here // its easier to read its the <> button.

i learnt to solve this problem with a dev c++ tutorial that i can no longer find...its easy; ignore all this for now and play with member object functions and sutch in your compiler, call the odd function learn get and set its actually quite fun to learn this way get help from buckys c++ youtube tutorials you will love him everyone does(he knows everything)

the devc++ tutorials are good because they talkt to you as though you were human...i think the guys who are good enough to write tutorials forget its hard to understand an explanation if you dont already know the language, the book c++ for dummies is a prime example its written as though you studied computer science for ten years
Last edited on
Do you know how to:
-Write friend functions
-Overload operators
-Overload the << operator for streams like std::cout

Which one do you need help with?
I know how to do all of them but i just genuinly dont know where to start with this problem, i have been working my way slowly through the book for almost 5 months now, taking a shitload of notes to make sure i understand the use of it. Its just that i dont know where to start.

Thanks devonrevenge, i didnt realise you could make your code display as code lol, my bad.
I have used bucky quite a bit but still after going over his tutorials, specifically overloading operators but i just confused on how to approach it
Book wrote:
Improve the Lobby class from the Game Lobby program by writing a friend function of the Player class that allows a Player object to be sent to cout. Next, update the function that allows a Lobby object to be sent to cout so that it uses your new function for sending a Player object to cout.
Work on this part first. You can quickly write a friend function that overloads operator << for std::ostream and Player.
Last edited on
Topic archived. No new replies allowed.