您的位置:首页 > 其它

菜单类型的重复选择,使用while做到能够一直选择

2014-03-21 16:01 246 查看
实例:

#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: ";
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐