< Previous | Contents | Next >
In main(), I instantiate a new Critter object. Because I don’t supply values for m_Hunger or m_Boredom, the data members start out at 0, and the critter begins life happy and content. Next, I create a menu system. If the player enters 0, the program ends. If the player enters 1, the program calls the object’s Talk() member function. If the player enters 2, the program calls the object’s Eat() member function. If the player enters 3, the program calls the object’s Play() member function. If the player enters anything else, he is told that the choice is invalid.
int main()
{
Critter crit; crit.Talk();
int choice; do
{
cout << "\nCritter Caretaker\n\n"; cout << "0 - Quit\n";
cout << "1 - Listen to your critter\n"; cout << "2 - Feed your critter\n";
cout << "3 - Play with your critter\n\n";
cout << "Choice: "; cin >> choice;
switch (choice)
{
case 0:
cout << "Good-bye.\n"; break;
case 1:
crit.Talk(); break;
case 2:
crit.Eat(); break;
case 3:
crit.Play(); break;
default:
cout << "\nSorry, but " << choice << " isn’t a valid choice.\n";
}
} while (choice != 0);
return 0;
}