Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,576 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,869 people online right now. Registration is fast and FREE... Join Now!




User-defined function doesnt work

 
Reply to this topicStart new topic

User-defined function doesnt work, I have an idea on how to write a user defined function they just don&#

cball1321
27 Sep, 2008 - 04:29 PM
Post #1

New D.I.C Head
*

Joined: 27 Sep, 2008
Posts: 24

This program is supposed to get the rate for the customer then compute the insurance premium. The rate must be obtained by using a user defined function. I'm not very good at these at all.

# include <iostream>
# include <iomanip>
# include <cstring>
# include <cmath>
# include <cctype>
using namespace std;


int main ()
{
const double MALE_RATE_1 = .095;
const double MALE_RATE_2 = .082;
const double MALE_RATE_3 = .074;
const double MALE_RATE_4 = .062;
const double FEMALE_RATE_1 = .080;
const double FEMALE_RATE_2 = .069;
const double FEMALE_RATE_3 = .058;
const double FEMALE_RATE_4 = .052;
const double OVER_1 = .051;
const double OVER_2 = .062;
const double OVER_3 = .075;
double vehicle_value, Premium, Rate;
int age;
char gender = "m" || "M" || "f" || "F";

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");
return 14;
}

else if (age > 14)
{
cout <<"Enter the driver's gender <M/F>: ";
cin >> gender;
cout << endl;
}

double Driver_Rate( double Rate);
{
double Rate;

if (age >= 14 && age <= 16 && (gender == 'M' || gender == 'm'))
Rate = MALE_RATE_1;
else if (age >= 17 && age <= 21 && (gender == 'M' || gender == 'm'))
Rate = MALE_RATE_2;
else if (age >= 22 && age <= 26 && (gender == 'M' || gender == 'm'))
Rate = MALE_RATE_3;
else if (age >= 27 && age <= 30 && (gender == 'M'|| gender == 'm' ))
Rate = MALE_RATE_4;
else if (age >= 14 && age <= 16 && (gender == 'F' || gender == 'f'))
Rate = FEMALE_RATE_1;
else if (age >= 17 && age <= 21 && (gender == 'F' || gender == 'f'))
Rate = FEMALE_RATE_2;
else if (age >= 22 && age <= 26 && (gender == 'F' || gender == 'f'))
Rate = FEMALE_RATE_3;
else if (age >= 27 && age <= 30 && (gender == 'F' || gender == 'f'))
Rate = FEMALE_RATE_4;
else if (age >= 31 && age <=64)
Rate = OVER_1;
else if (age >= 65 && age <=75)
Rate = OVER_2;
else if (age > 75)
Rate = OVER_3;

return Rate;
}

cout <<"Enter the value of the vehicle. ";
cin >> vehicle_value;
cout << endl;

Premium = vehicle_value / Rate;
cout << Premium;

cout << endl << endl;
system ("pause");
return 0;
}





User is offlineProfile CardPM
+Quote Post

OliveOyl3471
RE: User-defined Function Doesnt Work
27 Sep, 2008 - 09:23 PM
Post #2

It's all about the code ♥
Group Icon

Joined: 11 Jul, 2007
Posts: 1,636



Thanked: 18 times
Dream Kudos: 150
My Contributions
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;
}

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/3/08 12:08AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month