/* Contestant Database */ #include #include #include using namespace std; //constant definitions const int MAX_NUM_CONTESTANTS = 50; //class definitions class Name { public: string last; string first; }; class Job_info { public: string title; double salary; }; class Personal_info { public: char gender; string haircolor; int age; Job_info job; }; class Contestant{ Name name; Personal_info personal; public: // member functions bool readData(ifstream &cfile); void printData(ofstream &dbfile); bool compareAge(int agewanted); }; // Function Prototypes void readdata(Contestant[], int &); void printdata(Contestant[], int); void printmenu(void); void findage(Contestant[], int); void findgender(Contestant[], int); void findhair(Contestant[], int); void findtitle(Contestant[], int); void findsalary(Contestant[], int); void pause(void); int main() { Contestant contestant[MAX_NUM_CONTESTANTS]; int num; char choice; bool not_done = true; /* first part */ /* fill and print database */ readdata(contestant,num); printdata(contestant,num); /* second part */ /* call functions to read and process requests */ do { printmenu(); cin >> choice; switch(choice) { case 'q': case 'Q': not_done = false; break; case 'a': case 'A': findage(contestant,num); break; case 'g': case 'G': findgender(contestant,num); break; case 'h': case 'H': findhair(contestant,num); break; case 't': case 'T': findtitle(contestant,num); break; case 's': case 'S': findsalary(contestant,num); break; default: cout << "Invalid choice - try again\n\n"; break; } } while (not_done); //system("pause"); //un-comment if using devC++ return 0; } /* Function readdata() */ void readdata(Contestant contestant[], int &count) { // open input file ifstream cfile("c:\\bc\\cis1_5\\newpgms\\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 printdata: */ void printdata(Contestant contestant[], int num) { // open output file // ofstream dbfile("c:\\bc\\cis1_5\\newpgms\\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].printData(dbfile); dbfile.close(); return; } /* Function printmenu() */ void printmenu(void) { cout << endl << endl; 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 letter corresponding to that trait.\n\n"; cout << "To quit, select Q.\n\n"; cout << "\t****************************\n"; cout << "\t List of Choices \n"; cout << "\t****************************\n"; cout << "\t Q -- quit\n"; cout << "\t A -- age\n"; cout << "\t G -- gender\n"; cout << "\t H -- hair color\n"; cout << "\t T -- title\n"; cout << "\t S -- salary\n"; cout << endl << endl <<"\tEnter your selection: "; return; } /* Function findage() */ void findage(Contestant contestant[], int num) // add to main { 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 printData: */ /* Contestant member function printData(): * 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 Contestant::printData(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() */ /* Contestant 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 Contestant::compareAge(int agewanted) { if (personal.age == agewanted) { cout << name.first << " " << name.last << endl; return true; } return false; }