#include<iostream>usingnamespace std;intmain(){char temp ='0';
cout <<"Please enter one of the follonwing choices:\n"<<"c) carnivore\tp) pianist\nt) tree \tg) game\n";
cin >> temp;while((temp !='c')&&(temp !='p')&&(temp !='t')&&(temp !='g')){
cout <<"Please enter a c,p,t or g: ";
cin >> temp;}switch(temp){case'c':
cout <<"C !!\n";break;case'p':
cout <<"P !!\n";break;case't':
cout <<"T !!\n";break;case'g':
cout <<"G !!\n";break;}}
Method 2
#include<iostream>usingnamespace std;intmain(void){char ch;
cout <<"Please enter one of the following choices :"<< endl;
cout <<"c) carnivore "<<"\t p) pianist"<< endl;
cout<<" t) tree"<<" \tg) game"<< endl;while(cin >> ch){switch(ch){case'c':
cout <<"tiger is a carnivore."<< endl;break;case'p':
cout <<"Xiaoming is a pianist."<< endl;break;case't':
cout <<"A maple is a tree."<< endl;break;case'g':
cout <<"Golf is a game."<< endl;break;default:
cout <<"Please enter a c, p, t, or g: ";}}system("pause");return0;}
6.4
#include<iostream>usingnamespace std;constint strsize =20;structbop{char fullname[strsize];char title[strsize];char bopname[strsize];int preference;};intmain(){
bop bopArr[]={{"Wimp Macho","Driver","W",2},{"Raki Rhodes","Actor","R",1},{"Celia Laiter","Teacher","C",1}};
cout <<"BOP report"<< endl;
cout <<"a. display by name\t b.display by title\n"<<"c. display by bopname\t d. display by preference\n"<<"q. quit\n";char temp;
cout <<"Enter your choice: ";while(cin>>temp){if(temp =='q'){
cout <<"Bye! "<< endl;break;}switch(temp){case'a':for(int i =0; i <3; i++){
cout << bopArr[i].fullname << endl;}break;case'b':for(int i =0; i <3; i++){
cout << bopArr[i].title << endl;}break;case'c':for(int i =0; i <3; i++){
cout << bopArr[i].bopname << endl;}break;case'd':for(int i =0; i <3; i++){if(bopArr[i].preference==0)
cout << bopArr[i].fullname << endl;elseif(bopArr[i].preference ==1)
cout << bopArr[i].title << endl;elseif(bopArr[i].preference ==2)
cout << bopArr[i].bopname << endl;}break;default:"please enter the right char! \n";}
cout <<"Next choice: ";}}