This is the original question: IN-surance is a SA company providing insurance for class A cars. You are required to write a program named question5.cpp to assist the company in calculating the monthly premiums for its customers. The customers should pay a standard fee of R850.00 per month. In addition to the standard fee, the following fees are applicable:
Experience: Experienced drivers pay less than less experienced drivers as follows: o 0 – 2 years pay R150.00 extra o 3 – 5 years pay R80.00 extra o 6 and more years pay only R30 extra.
Age: Younger people pay more than older citizens as follows: o 18 – 28: pays R50.00 extra. o 29 – 45: pays R30.00 extra. o 46 – 60 : pays R20.00 extra. o 61 and above pays R30.00 extra.
Gender: Male customers pay an additional R10.00.
Marital status: People who are single or divorced (available) pay more that those who are married or living with their partners (not available). o Available pays R40.00 extra.
Write the following functions: driversGroup: This function takes as its parameter driving experience and it returns the additional fee that the customer will have to pay.
ageGroup: This function takes as its parameter the age of the driver and it returns the additional fee that the customer will have to pay. The age should not be less than 18 years.
isMale: This function takes as its parameter the gender of the driver and it returns a true value if it is a male. The function should only accept ‘M’ or ‘m’ for Male and ‘F’ or ‘f’ for female. COS1511/101 99 isAvailable: This function takes as its parameter the marital status of the driver and it returns a true value if the customer is Single or Divorce. The function should only accept ‘S’ or ‘s’ for single, ‘D’ or ‘d’ for divorces, ‘M’ or ‘m’ for married, and ‘L’ or ‘l’ for living with a partner.
computePremium: The function takes as its parameters the experience, age, gender and marital status and return the total premium to be paid. This function should use the functions that you wrote above.
Add a main() function to demonstrate the use of these functions.
My attempt at the question I achieve most objectives except for the second last one where I have to compute the premium where I used addition, but for the life of me I keep getting an answer of 0 from the addition of the return values of the other function but as you can see I used solo function calls where values are given for the function but when I use them in a function together they return the value 0.
The Code: //Insurance program that calculates insurance premium based on age,gender,years driven and marital status.
#include <iostream>
using namespace std;
int driversGroup(int yearsDriven)
{
int d=0;
if(yearsDriven <= 2)
d += 150;
if(yearsDriven <= 5 && yearsDriven >= 3)
d += 80;
if(yearsDriven > 5)
d += 30;
return d;
}
int ageGroup(int age,int agePremium)
{
if(age >= 18)
{
if(age <= 28 && age >= 18)
agePremium += 50;
if(age <= 45 && age >= 29)
agePremium += 30;
if(age <= 60 && age >= 46)
agePremium += 20;
if(age > 60)
agePremium += 30;
}
else
cout<< "The individual is too young. " << endl;
return agePremium;
}
int isMale(char sex, int sexPremium)
{
switch(sex)
{
case 'm':
sexPremium += 10;
break;
case 'M':
sexPremium += 10;
break;
case 'f':
sexPremium = 0;
break;
case 'F':
sexPremium = 0;
break;
}
return sexPremium;
}
int isAvailable(char maritalStatus)
{
int e=0;
switch(maritalStatus)
{
case 's':
e = true;
break;
case 'S':
e = true;
break;
case 'd':
e = true;
break;
case 'D':
e = true;
break;
case 'l':
e = false;
break;
case 'L':
e = false;
break;
case 'm':
e = false;
break;
case 'M':
e = false;
break;
}
if(e = 1)
{
cout << "true ";
}
else
{
cout << "false ";
}
return e;
}
int computePremium(int d,int agePremium,int sexPremium)
{
int r=0;
r = d + agePremium + sexPremium;
cout << r;
return r;
}
int main()
{
int d=0
,agePremium=0
,sexPremium=0
,yearsDriven=0
,age=0
,r=0;
char maritalStatus, sex;
cout << "This program will calculate your insurance premium\n" << endl;
cout << "Please enter how many years you have been driving: " << endl;
cin >> yearsDriven;
cout << "Please enter what age you are: " << endl;
cin >> age;
cout << "Please enter sex you are ((m)ale,(f)emale): " << endl;
cin >> sex;
cout << "Please enter your marital status ((S)ingle,(D)ivorced,(L)iving together,(M)arried): " << endl;
cin >> maritalStatus;
cout << "Driversgroup " <<driversGroup(yearsDriven) << endl;
cout << "agegroup " << ageGroup(age,agePremium) << endl;
cout << "ismale "<<isMale(sex,sexPremium)<< endl;
cout << "is available "<< isAvailable(maritalStatus) << endl;
cout << "computePremium " << computePremium(d,agePremium,sexPremium) << endl;
cout << "Total cost of your premium is: " << r;
return 0;
}
Aucun commentaire:
Enregistrer un commentaire