Hello all. I'm trying to write a program to calculate the cost of car insurance based on these criteria:
"Male drivers under the age of 21 pay a surcharge of 17%, while female drivers under the age of 21 pay a surcharge of 4% of the base charge. A driver with more than 3 tickets pays an extra $100.
Write a program to calculate insurance rates based on this information. The information will be entered by the user and passed as arguments (actual parameters) to the function. The result will be returned to the main program by the function and displayed. Use a loop in the main program so that the user has the option of running the program as many times as desired."
So far, I haven't figured out how to put the calculation into the function yet, so it looks real messy. The warning listed when I compile is "'Insurance_Cost' : recursive on all control paths, function will cause runtime stack overflow."
Below are the parts I have written so far.
Can someone please point out what I need to fix? Any help is appreciated. Thanks in advance!
Code:
#include <iostream>
#include <iomanip> // for use with setf(), setw() and setprecision()
using namespace std; // Used in place of std::cout, std::cin, etc.
float Insurance_Cost(int age, int ticket_history, char gender, float car_value);
//************************************************************************
float Insurance_Cost(int age, int ticket_history, char gender,
float car_value)
{
float insCost;
insCost = Insurance_Cost(age, ticket_history, gender, car_value);
return insCost;
}
//************************************************************************
void DisplayResult(int age, int ticket_history, char gender, float car_value)
{
cout << "The insurance cost for a " << age << "year old driver with "
<< ticket_history << "is: $"
<< Insurance_Cost(age, ticket_history, gender, car_value) << endl;
}
//************************************************************************
int main ()
{
char gender;
int age, ticket_history;
float base_rate, car_value, ticket_fee, cost;
cout.setf(ios::fixed | ios::showpoint);
cout << setprecision(2);
do
{
cout << "This program calculates the cost of car insurance." << endl
<< endl;
cout << "Please enter the value of the car: $";
cin >> car_value;
cout << "Please enter the age of the driver: ";
cin >> age;
cout << "Please enter the gender of the driver (M or F): ";
cin >> gender;
cout << "Please enter the number of tickets the driver has: ";
cin >> ticket_history;
//<< Insurance_Cost(age, ticket_history, gender, car_value) << endl;
//DisplayResult(age, ticket_history, gender, car_value);
base_rate = car_value*0.06F;
// insurance_cost = base_rate + ticket_fee;
if (age < 21)
{
if (gender == 'M')
{
cost = base_rate*1.17F;
if (ticket_history > 3)
{
ticket_fee = 100.00F;
}
else if (ticket_history <= 3)
{
cost = base_rate;
}
}
else if (gender == 'F')
{
cost = base_rate*1.04F;
if (ticket_history > 3)
{
ticket_fee = 100.00F;
}
else if (ticket_history <= 3)
{
cost = base_rate;
}
}
}
else if (age >= 21)
{
cost = base_rate;
if (ticket_history > 3)
{
ticket_fee = 100.00F;
}
else if (ticket_history <= 3)
{
cost = base_rate;
}
}
} while (car_value > 0);
system ("pause");
//system ("cls");
return 0;
}