/* Contestant Database - with member functions */ #include #include #include #include //needed to use system() function using namespace std; //constant definitions const int NUMCONS = 50; //structure definitions struct Conname { string last; string first; }; struct Jobinfo { string title; double salary; }; struct Persinfo { char gender; string haircolor; int age; Jobinfo job; }; class Con { // member variables Conname name; Persinfo personal; public: // member functions bool readdata(ifstream &cfile); void prettyprint(ofstream &dbfile); bool compareage(int agewanted); }; // Function Prototypes void readdata(Con [], int &); void prettyprint(Con [], int); void printmenu(void); int selecttrait(Con [], int); void findage(Con [], int); void findgender(Con [], int); void findhair(Con [], int); void findtitle(Con [], int); void findsalary(Con [], int); void pause(void); int main() { Con contestant[NUMCONS]; int num; /* first part */ /* fill and print database */ readdata(contestant,num); prettyprint(contestant,num); /* second part */ /* call functions to read and process requests */ do { printmenu(); } while (selecttrait(contestant,num) != 0); //system("pause"); //un-comment when using DevC++ return 0; } /* Function readdata() */ void readdata(Con contestant[], int &count) { // open input file ifstream cfile("c:\\bc\\CISC1110\\pgms\\chapter10\\myinput.txt"); //comment-out for debugging //ifstream cfile("con"); //un-comment for debugging count = 0; //initialize count while (contestant[count].readdata(cfile)) count++; cfile.close(); return; } /* Function prettyprint: */ void prettyprint(Con contestant[], int num) { // open output file //ofstream dbfile("c:\\bc\\CISC1110\\pgms\\chapter10\\myoutput.txt"); //comment-out for debugging ofstream dbfile("con"); //un-comment for debugging dbfile << "\t\tContestants in the Database\n\n"; dbfile << "Name\t\tGender\tHair\tAge\tTitle\tSalary\n\n"; for (int count = 0; count < num; count++) contestant[count].prettyprint(dbfile); dbfile.close(); return; } /* Function printmenu() */ void printmenu(void) { cout << "\n\n\n\n\n\n\n"; cout << "To obtain a list of contestants with a given\n"; cout << "trait, select a trait from the list and type in\n"; cout << "the number corresponding to that trait.\n\n"; cout << "To quit, select 0.\n\n"; cout << "\t****************************\n"; cout << "\t List of Choices \n"; cout << "\t****************************\n"; cout << "\t 0 -- quit\n"; cout << "\t 1 -- age\n"; cout << "\t 2 -- gender\n"; cout << "\t 3 -- hair color\n"; cout << "\t 4 -- title\n"; cout << "\t 5 -- salary\n"; cout << "\n\n\tEnter your selection, 0-5: "; return; } /* Function selecttrait() */ int selecttrait(Con contestant[], int num) { int choice; do { cin >> choice; switch(choice) { case 0: break; case 1: findage(contestant,num); break; case 2: findgender(contestant,num); break; case 3: findhair(contestant,num); break; case 4: findtitle(contestant,num); break; case 5: findsalary(contestant,num); break; default: cout << "Incorrect value; try again\n"; cout << "\n\tEnter your selection, 0-5: "; break; } } while (choice < 0 || choice > 5); return (choice); } /* Function findage() */ void findage(Con contestant[], int num) { int agewanted,found=0; cout << "\n\nEnter the age you want: "; cin >> agewanted; cout << "\nContestants whose age is " <> name.last) { cfile >> name.first; cfile >> personal.gender; cfile >> personal.haircolor; cfile >> personal.age; cfile >> personal.job.title; cfile >> personal.job.salary; return true; } return false; } /* Member Function prettyprint: */ /* Con member function prettyprint(): * Input: * dbfile - a reference to the output file * Process: * write the object's data members to the output file * Output: * the written data members of the object */ void Con::prettyprint(ofstream &dbfile) { dbfile.setf(ios::fixed,ios::floatfield); dbfile.precision(2); //set decimal precision dbfile << name.first; dbfile << "\t" << name.last; dbfile << "\t" << personal.gender; dbfile << "\t" << personal.haircolor; dbfile << "\t" << personal.age; dbfile << "\t" << personal.job.title; dbfile << "\t"; dbfile.width(9); dbfile << personal.job.salary; dbfile << endl; return; } /* Member Function compareage() */ /* Con member function compareage(): * Input: * agewanted - age wanted * Process: * compare agewanted to the object's age * Output: * if ages are the same, print the object's name and return true * else return false */ bool Con::compareage(int agewanted) { if (personal.age == agewanted) { cout << name.first << " " << name.last << endl; return true; } return false; }