< Previous | Contents | Next >

The main() Function

The main() function displays the players in the lobby, presents the user with a menu of choices, and performs the requested action.

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;

Summary 325



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


return 0;

}

The function first instantiates a new Lobby object, and then it enters a loop that presents a menu and gets the users choice. Then it calls the corresponding Lobby objects member function. If the user enters an invalid choice, he or she is told so. The loop continues until the user enters 0.