编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第6章编程练习4
#include <iostream> using namespace std; const int strsize=30; const int BOPSIZE=5; void showmenu() { cout<<"Benevolent Order of Programmers Report\n" "a. display by name b. display by title\n" "c. display by bopname d. display by preference\n" "q. quit\n"; } struct bop { char fullname[strsize]; char title[strsize]; char bopname[strsize]; int preference; }; int main() { showmenu(); char ch; int i; bop newbop[BOPSIZE]= { {"Wimp Macho","Teahcer","WWW",0}, {"Raki Rhodes","Junior Programmer","RRR",1}, {"Celia Laiter","Star","MIPS",2}, {"Hoppy Hipman","Analyst Trainee","HHH",1}, {"Pat Hand","Doctor","LOOPY",2} }; cout<<"Enter your choice: "; while(cin>>ch && ch!='q') { switch(ch) { case 'a': for(i=0;i<BOPSIZE;i++) cout<<newbop[i].fullname<<endl; break; case 'b': for(i=0;i<BOPSIZE;i++) cout<<newbop[i].title<<endl; break; case 'c': for(i=0;i<BOPSIZE;i++) cout<<newbop[i].bopname<<endl; break; case 'd': for(i=0;i<BOPSIZE;i++) { if(0==newbop[i].preference) cout<<newbop[i].fullname<<endl; else if(1==newbop[i].preference) cout<<newbop[i].title<<endl; else if(2==newbop[i].preference) cout<<newbop[i].bopname<<endl; } break; //case 'q':cout<<"Bye!\n"; } cout<<"Next choice: "; } cout<<"Bye!\n"; system("pause"); return 0; }