------------------------------------------------------------- //JimStroud //CS 201, Section 6 //Extra Credit: Problem 4, pg. 705 //*********************************************************** //Program premium.cpp prints a list of a life insurance // companies customers and the premium that each customer // pays. Premiums are based on the age the customer was when // he became a customer. The following table is used to //determine each customers premium, but these rates are // subject to change; //Age Premium // 25 277.00 // 35 287.00 // 45 307.00 // 55 327.00 // 65 357.00 //Each age listed in this table is the upper limit for the // premium. For example, if a customer signed up for a //policy when she was 37, she would pay $307.00. //Input: The table listed above is in one file and the //customers names and ages are in another file. The // premium file is read into two parallel arrays and the // customers names and ages are read into another pair of //parallel arrays. The two data items per line in each file //are to be separated by a blank and each line ends with a //newline character. Although it is treated as one data //item by the program, the customer's first and last names //are to have a blank between them. //Output: Print to standard output a formatted, labeled //list showing each customers name, his age when the policy // was bought, and the customers premium. //Assumptions: The data is formatted as described in the // input description. The two parallel arrays that are used // to read the data from each file are of equal length. //*********************************************************** #include #include #include #include #include "boolean.h" typedef char String40[ 41 ]; typedef char String2[ 3 ]; //Maximum customer name characters. const int MAX_NAME_LENGTH = 21; //Maximum number of customers that can be processed. const int MAX_CUSTOMERS = 50; //Maximum number of insurance premium categories. const int MAX_PREMIUM_CATEGORIES = 5; //PRINT Output header void OutputHeader( void ); //Find the premium for a particular cusotmer. void GetPremium( const int, const String2[], const String40[], const int, double&, Boolean& ); //Input data into two parallel arrays of type character. void InputFileData( ifstream&, String2[], String40[], int&, const int ); //Open a file for input. void OpenInputFile( ifstream& ); int main() { ifstream premiumFile, customerFile; String40 customerName[ MAX_CUSTOMERS ]; String2 customerAge[ MAX_CUSTOMERS ]; String40 premiumAmount[ MAX_PREMIUM_CATEGORIES ]; String2 premiumAge[ MAX_PREMIUM_CATEGORIES ]; Boolean premiumFound; int premiumLength; //Length of premium array. int customerLength; //Length of customer array. int countCustomers; double customerPremium; //Some output formatting cout.setf( ios::fixed, ios::floatfield ); cout.setf( ios::showpoint ); cout << endl << endl << endl << endl << endl; //OPEN the age-premium file cout <<"Input the premium data file name: "; OpenInputFile( premiumFile ); if( !premiumFile ) return 1; //Input the age-premium file data into two arrays. InputFileData( premiumFile, premiumAge, premiumAmount, premiumLength, MAX_PREMIUM_CATEGORIES ); //OPEN the customer name-age file cout <<"Input the customer data file name: "; OpenInputFile( customerFile ); if( !customerFile ) return 1; //Input the customer name-age file data into two arrays. InputFileData( customerFile, customerAge, customerName, customerLength, MAX_CUSTOMERS ); //Print output header OutputHeader(); for( countCustomers = 0; countCustomers < customerLength; countCustomers++ ) { //Get the premium. GetPremium( atoi( customerAge[ countCustomers ] ), premiumAge, premiumAmount, premiumLength, customerPremium, premiumFound ); //Output the customer insurance information cout << setw( MAX_NAME_LENGTH ) << customerName[ countCustomers ] << setw( 6 ) << customerAge[ countCustomers ]; if( premiumFound ) cout << setprecision( 2 ) << setw( 11 ) << customerPremium << endl; else //A premium that is not found is undefined. cout << setw( 11 ) << "undefined" << endl; } return 0; }//end main(). //*********************************************************** //PRINT Output header void OutputHeader( void ) //Post: A header for the customer insurance info is printed. { cout << endl << endl << setw( MAX_NAME_LENGTH ) << "Customer's Name" << setw( 6 ) << "Age" << " Premium" << endl; for( int i = 0; i < MAX_NAME_LENGTH + 17; i++ ) cout << '-'; cout << endl; }//end OutputHeader(); //*********************************************************** //Open a file for input. void OpenInputFile( /*IN-OUT*/ ifstream& someFile ) //Pre: the user has been prompted for appropriate input. //Post: someFile has been opened // OR the error message, "Couldn't open file.", printed. { char fileName[ 15 ]; cin >> fileName; someFile.open( fileName ); if( !someFile ) cout << "Couldn't open file " << fileName << '!'; }//end OpenInputFile(). //*********************************************************** //Input data from a file into two parallel arrays of type character. void InputFileData( /*IN*/ ifstream& fileName, /*IN-OUT*/ String2 array1[], /*IN-OUT*/ String40 array2[], /*IN-OUT*/ int& length, //length of array /*IN*/ const int maxLength)//max array component= s //Pre: fileName has been assigned, array1 and array2 are // declared, AND maxlenth has been assigned the value // of the maximum length of these arrays. //Post: length has been assigned a value equal to the // length of the arrays, // && array1[ 0..length-1 ], array2[ 0..length-1 ] have // strings assigned to them // && length == the number of components in each array // && fileName stream has failed because of EOF // || filename stream still is open and still containss // data items for which a warning message has been printed. { char dummy; //input dummy for newline character(value discarded) //Initialize the loop and process. length = 0; fileName >> array1[ length ]; while( fileName ) { //Get array2[ 0...length-1] fileName.get( array2[ length ], MAX_NAME_LENGTH ); fileName.get( dummy ); if( fileName ) length++; //Get array1[ 1...length-1] fileName >> array1[ length ]; //If the file is still open and the maximum number of //array components have been filled, print warning. if( fileName && length == maxLength ) cout << "This file exceeds the maximum number of " << "data items." << endl << "Only " << maxLength << " lines of data will" << " be processed." << endl; } }//end InputFileData. //*********************************************************** //Find the premium for a particular customer. void GetPremium( /*IN*/ const int custAge, /*IN*/ const String2 premiumAge[], /*IN*/ const String40 premiumAmount[], /*IN*/ const int length, /*OUT*/ double& customerPremium, /*IN-OUT*/ Boolean& premFound ) //Pre: All constant formal paramaters are assigned // && length has been assigned the common length of // the two arrays. //Post: customerPremium is assinged the customer's premium // && premFound is assigned TRUE // || customerPremium is undefined // && premFound is assigned FALSE. { int index; //Indexes the premium loop. int premAge1; //First of two premium age limits. int premAge2; //second of two premium age limits. //Initialize process premFound = FALSE; index = 0; //A loop to determine the premium value for a customer. while( !premFound && index + 1 < length ) { premAge1 = atoi( premiumAge[ index ] ); premAge2 = atoi( premiumAge[ index + 1 ] ); //If a customer is as young as the first age value... if( index == 0 && custAge <= premAge1 && custAge >= 0 ) { customerPremium = atof( premiumAmount[ index ] ); premFound = TRUE; } //If a customer is between two age values... else if( custAge > premAge1 && custAge <= premAge2 ) { customerPremium = atof( premiumAmount[ index + 1 ] ); premFound = TRUE; } //If the customer's age value has not been located... else index++; } }//end GetPremium. data file 1: ------------------------------------------------------------- 25 277.00 35 287.00 45 307.00 55 327.00 65 357.00 data file 2: ------------------------------------------------------------- 30 Jim Stroud 36 Kathi Healy 64 George Jones 17 Bartholomew Miserly 25 James Brown 5 Samantha Healy 45 Duncan Donut 55 Spaghetti Head