#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;
}
【C++】枚举量注意
最新推荐文章于 2021-01-23 16:20:05 发布