您的位置:首页 > 编程语言 > C语言/C++

【C++】枚举量注意

2015-10-19 12:38 549 查看
#include<iostream>
#include<string> //头文件是string而不是cstring!!
//enum {c,C,p,P,t,T,g,G}; //定义常量,cin无法识别枚举类型,因此应输入整数。如输入0;代表c。枚举量提升为int类型

int main()
{
using namespace std;
char ch;
string strc="carnvore",strp="pianist",strt="tree",strg="game";
cout <<"Please enter one of the following choices :\n";
cout <<"c) "<<strc<<"\t\tp) "<<strp<<endl;
cout <<"t) "<<strt<<"\t\t\tg) "<<strg<<endl;
cin >>ch;
while(ch!='c'&&ch!='C'&&ch!='p'&&ch!='P'&&ch!='t'&&ch!='T'&&ch!='g'&&ch!='G')
{
cout <<"Please enter a c, p, t or g:";
cin >>ch;
}
cout <<"A maple is a ";
switch(ch)
{
case 'C' :
case 'c' : cout <<strc;break;
case 'p' :
case 'P' : cout <<strp;break;
case 'T' :
case 't' : cout <<strt;break;
case 'G' :
case 'g' : cout <<strg;break;
}
cout <<"."<<endl;
return 0;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: