实例:
#include <iostream>
using namespace std;
void showMenu();
int main()
{
char ch;
showMenu();
cin >> ch;
while(ch != 'q')
{
if(ch != 'c' && ch != 'p' && ch != 't' && ch != 'g')
{
cout << "Please enter a c, p, t, or g: ";
cin >> ch;
}
switch(ch)
{
case 'c': cout << "ccccc\n"; break;
case 'p': cout << "I am a pianist.\n"; break;
case 't': cout << "A maple is a tree.\n"; break;
case 'g': cout << "This is a game.\n"; break;
}
showMenu();
cin >> ch;
}
cout << "Bye!\n";
return 0;
}
void showMenu()
{
cout << "Please enter one of following chocies:" << endl;
cout << "c) carnivore p) pianist\n"
<< "t) tree g) game\n"
<< "q) quit";
cout << "Please enter a c, p, t, or g: ";
}