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.
//Game Lobby
//Simulates a game lobby where players wait
#include <iostream>
#include <string>
usingnamespace 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;
}
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
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
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.