The reason it was not workng is most likely because you cannot define a function within int main().
I have changed a few things to make the program work right. See the comments in the code. Delete them and
use your own, since they are mostly directed toward you, so you could see what I did.
You did not really need all those variables for the different rates. You really only need variables if you are going
to do something else with that same value later. We only use it one time in the program, to determine which rate
to return from the function. So you don't need to store all those different rates in variables.
You also didn't need some of those header files. The program runs fine now, but you may want to double check the math.
I did not go over that part too well, although I
think it is correct now.
cpp
# include <iostream>
# include <iomanip>
using namespace std;
//always define a function outside of int main
//pass Rate into the function by reference so its value can be changed by the function
double Driver_Rate(double &Rate, int age, char gender)
{
if (age >= 14 && age <= 16 && (gender == 'M' || gender == 'm'))
Rate = .095;
else if (age >= 17 && age <= 21 && (gender == 'M' || gender == 'm'))
Rate = .082;
else if (age >= 22 && age <= 26 && (gender == 'M' || gender == 'm'))
Rate = .074;
else if (age >= 27 && age <= 30 && (gender == 'M'|| gender == 'm' ))
Rate = .062;
else if (age >= 14 && age <= 16 && (gender == 'F' || gender == 'f'))
Rate = .080;
else if (age >= 17 && age <= 21 && (gender == 'F' || gender == 'f'))
Rate = .069;
else if (age >= 22 && age <= 26 && (gender == 'F' || gender == 'f'))
Rate = .058;
else if (age >= 27 && age <= 30 && (gender == 'F' || gender == 'f'))
Rate = .052;
else if (age >= 31 && age <=64)
Rate = .051;
else if (age >= 65 && age <=75)
Rate = .062;
else if (age > 75)
Rate = .075;
return Rate;
}
int main ()
{
double vehicle_value, Premium=0, Rate = 0;
int age;
char gender; //no need to initialize this here, you'll get it from the user anyway
cout << fixed << setprecision(2);
cout <<"Enter the driver's age: ";
cin >> age;
cout << endl;
if (age < 14)
{
cout <<"!!TOO YOUNG TO DRIVE!! " << endl;
system("pause");
}
else if (age > 14)
{
cout <<"Enter the driver's gender <M/F>: ";
cin >> gender;
cout << endl;
}
cout <<"Enter the value of the vehicle. ";
cin >> vehicle_value;
cout << endl;
//assign value returned by function to variable 'Rate'
Rate=Driver_Rate(Rate, age, gender); //call the function like this
Premium = vehicle_value * Rate;//I changed this to multiply, seems like it's right now
cout << Premium;
cout << endl << endl;
system ("pause");
return 0;
}