C++PrimerPlus的程序问题,大神来看看

sinat_30330131 2015-11-20 11:00:25
题目:
编写一个菜单驱动程序,该程序显示一个提供4个选项的菜单---每个选项用一个字母标记。如果用户使用有效选项之外的字母进行响应,程序将提示用户输入一个有效的字母,指导用户这样做为止。然后,改程序使用一条switch语句,根据用户的选择进行一个简单操作。

程序应该是这样的:
Please enter one of the following choices:
c)carnivore p)pianist
t)tree g)game
Please enter a c,p,t, or g: q
Please enter a c,p,t, or g: t
A maple is a tree.


#include<iostream>
using namespace std;
void menu();
int main()
{
menu();
char ch;
cin.get(ch);
while (ch != EOF)
{
while (ch!='c' && ch!='p' && ch!='t' && ch!='g')
{
cout << "Please enter a c,p,t, or g: ";
cin.get(ch);
}
switch(ch)
{
case 'c': cout << "A maple is a carnivore.\n";
break;
case 'p': cout << "A maple is a pianist. \n";
break;
case 't': cout << "A maple is a tree. \n";
break;
case 'g': cout << "A maple is a game. \n";
break;
}
cin.get(ch);
}
return 0;
}
void menu()
{
cout << "Please enter one of the following choices: \n";
cout << "c)carnivore\t" << "p)pianist\n";
cout << "t)tree\t" << "g)game\n";
}

实际运行后出现如下错误,麻烦大神帮忙看一下,真看不懂。。
...全文
126 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2015-11-20
  • 打赏
  • 举报
回复
27行前加cin.clear();

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧