// PROGRAM: dcheung_goodhand.cpp // PROGRAMMER: David Cheung // DATE: 11/07/06 // PURPOSE: Calculates insurance rates based on driver criteria // INPUT: Driver's age, ticket history and value of the car // OUTPUT: Purpose of the program // Insurance rate for the individual // DESCRIPTION: /* Algorithm: * * main() function * { * Input age of driver * * While age >= 0 * Input gender * Input ticket history * Input car value * * call functionCalculateInsurance() * * Output cost * Input age * End while loop * } * * CalculateInsurance() function * { * premium = car value * base rate * * if age < 21 * if gender = f * premium = premium * female U21 penalty * else * premium = premium * male U21 penalty * * if tickets > 3 * premium = premium + bad driver penalty * * return premium * } */ #include #include using namespace std; // Declare function prototype float functionCalculateInsurance(int, int, char, float); // Declare other constants const float constBaseRate = 0.06; // base rate to be multiplied to the vehicle value const float constFemaleU21 = 1.04; // rate increase for women under 21 const float constMaleU21 = 1.17; // rate increase for men under 21 const int constCitationLimit = 3; // maximum # of citations before penalty kicks in const int constBadDrivers = 100; // bad driver penalty int main() { int varAge; // Age of driver, varMaturity int varTickets; // Number of tickets, varCitations char boolGender; // Gender of driver, boolSex float varCost; // Value of the vehicle, varValue float varRate; // Insurance premium // Use a fixed number notation and show the decimal point cout << fixed << showpoint; // Start the program cout << "Welcome to your Insurance Premium Calculator!" << endl; cout << endl << "What is the driver's age (negative # to quit)? "; cin >> varAge; // input the age of driver while (varAge >= 0) { cout << endl << "Is the driver male or female (m/f)? "; cin >> boolGender; // input the gender of driver, lowercase only // error checking the gender while (boolGender != 'm' && boolGender != 'f') { if (boolGender == 'M') boolGender = 'm'; else if (boolGender == 'F') boolGender = 'f'; else { cout << endl << "Please specify male or female (m/f): "; cin >> boolGender; } } cout << endl << "How many citations for the driver? "; cin >> varTickets; // input the number of citations // error checking the citations while (varTickets < 0) { cout << endl << "Please enter a positive number: "; cin >> varTickets; } cout << endl << "What is the vehicle's value? "; cin >> varCost; // input the vehicle's value // error checking the vehicle value while (varCost < 0) { cout << endl << "Please enter a value greater than or equal to zero: "; cin >> varCost; } // Calculate the insurance rate varRate = functionCalculateInsurance(varAge, varTickets, boolGender, varCost); cout << setprecision(2) << "The total insurance premium for this driver is $" << varRate << endl; cout << endl << "What is the driver's age (negative # to quit)? "; cin >> varAge; // input the age of the driver for while loop } cout << "Thank you for using the Insurance Premium Calculator. Goodbye!"; return 0; } /* FUNCTION * NAME: functionCalculateInsurance() * DESCRIPTION: Calculates the insurance premium for main() * PROTOTYPE: float functionCalculateInsurance(int, int, char, float) * PARAMETERS: 2 integers, a character, and a floating pt number * RETURN VALUE: an integer */ float functionCalculateInsurance( int varMaturity, // Age of driver, varAge int varCitations, // Number of tickets, varTickets char boolSex, // Gender of driver, boolGender float varValue // Value of the vehicle, varCost ) { float varPremium; // this is the insurance rate to be returned varPremium = varValue * constBaseRate; // Check to see if the driver is under 21 if (varMaturity < 21) { // Check to see if the customer is a woman if (boolSex == 'f') varPremium = varPremium * constFemaleU21; // if it's a guy, then it's more expensive, sorry bro. else varPremium = varPremium * constMaleU21; } // Check to see if the driver has more than 3 citations if (varCitations > constCitationLimit) { varPremium = varPremium + constBadDrivers; // bad drivers pay more } return varPremium; }