编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第6章编程练习9
#include <iostream> #include <fstream> #include <cstdlib> #include <string> using namespace std; struct patron { double mon; string name; }; int main() { int num,count1=0,count2=0; ifstream fin; char filename[20]; cout<<"Enter name of data file: "; cin.get(filename,20); fin.open(filename); if(!fin.is_open()) { cout<<"Could not to open the file "<<filename<<endl; cout<<"Program termination.\n"; exit(EXIT_FAILURE); } fin>>num; fin.get(); patron *newp=new patron[num]; for(int i=0;i<num;i++) { getline(fin,newp[i].name); fin>>newp[i].mon; fin.get(); } cout<<"Grand Patrons:\n"; for(int j=0;j<num;j++) { if(newp[j].mon>10000) { cout<<newp[j].name<<":"<<newp[j].mon<<endl; count1++; } if(count1==0) cout<<"none\n"; cout<<"Patrons:\n"; for(int k=0;k<num;k++) { if(newp[k].mon<=10000) { cout<<newp[k].name<<":"<<newp[k].mon<<endl; count2++; } } if(count2==0) cout<<"none\n"; } delete [] newp; system("pause"); return 0; }