Please help a Noob out...

Hello World

I am at a loss with c++, Please help.
The totalpremium is calculated wrong and the check for right input on marital also fails.

I have the following homework assigment:

QUESTION 5: FUNCTIONS ONE OR MORE VALUE PARAMETERS

IN-surance is providing insurance for class A cars. You are required to write a program 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 divorces (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:
a) driversGroup: This function takes as its parameter driving experience and it returns the additional fee that the customer will have to pay.
72
b) 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.
c) 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.
d) 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.
e) 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.
f) Write program named to demonstrate the use of these functions.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <cmath>
#include <string>

using namespace std;

// Takes driving exp and returns the additional fee
int driversgroup(int drivingXp)
{
    if (drivingXp>0 && drivingXp<2)
    {
       return 150;}
    else if (drivingXp>3 && drivingXp<5)
    {
        return 80;}
    else if(drivingXp>6){
        return 30;}
}
// Takes the driving age and returns the additional fee must not be <18
int ageGroup(int driverage)
{
    if (driverage>18 && driverage<28){
    return 50;}
    else if (driverage>29 && driverage<45){
    return 30;}
    else if (driverage>46 && driverage<60){
    return 20;}
    else if (driverage>61){
    return 30;}


}
//Takes the gender of driver and test for male and return additnal fee
char isMale(char gender)
{
 if (gender =='M' || gender== 'm')
 {
     return 10;
 }
 else
 {
     return 0;
 }
}
//Takes the marital status of driver and returns addtional fee
int isAvailable(int marital)
{
 if (marital =='S' || 's'|| 'D'|| 'd'){
 return 40; }
}
// Computes Total premium paid
int computePremium(int driverage, int drivingXp, int gender, int marital)
{


    int Totalpremium= driversgroup(drivingXp) + ageGroup(driverage) + isMale(gender) + isAvailable(marital);

    cout << "Your Total premium is R" << Totalpremium << endl;
}

int main()
{
    int driverage;
    int drivingXp;
    char gender;
    char marital;

 cout << "What is your age?";
 cin >> driverage;
 cout << "How long have you been driving? (in years)";
 cin  >> drivingXp;
 cout << "Are you male or female? (M or F)";
 cin >> gender;
 cout << "Are you married?(S for single, D for divorce, M for married or L for living with partner)";
 cin >> marital;
 If(marital != 'm'||'M'||'d'||'D'||'s'||'S'||'l'||'L'){
 cout << "Try again, invalid entry";
 cin >> marital;}

 computePremium(driverage,drivingXp,gender,marital);
}
(marital != 'm'||'M'||'d'||'D'||'s'||'S'||'l'||'L') Common error
it parsed as (marital != 'm') || ('M') || etc... As 'M' has a non-zero character code, it would be treated as true.

You should compare against each value explicitely:
1
2
marital = tolower(marital); //Twice as less possibilities
if(marital != 'm' || marital != 'd' || etc...)


Same problem with isAvaliable function, but additionally you do not return anything if check was not passed.
if (marital =='S' || 's'|| 'D'|| 'd')

You need to include the complete marital == value for each comparison you do here.

Edit: Also here - same thing, and here I think you mean to use the && operator instead of ||.
If(marital != 'm'||'M'||'d'||'D'||'s'||'S'||'l'||'L')

c) 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.


Right now, you're returning a char from the isMale function - looks like it's supposed to be a bool.

Driversgroup and agegroup - the values at the boundaries of the number ranges aren't getting caught. For example - if someone is 28 - they don't get caught in any of the if/else statements.

Edit: you could also flip the order of the checks like this...
1
2
3
4
5
6
7
8
9
    if (driverage >= 61)
        return 30;
    else if (driverage >= 46)
        return 20;
    else if (driverage >= 29)
        return 30;
    else if (driverage >= 18)
        return 50;
    //else - what if they enter an age less than 18? 


Line 76-78 - what if they don't enter it correctly on the second try? I might use a while loop here, so that while the value is not valid, the user gets prompted to re-enter.
Last edited on
Awesome tips!! And what speed.

Thank You !!

I am tired and missed out on both problems.
Back to the old code pit for another swing at it.

After a quick return I made it work without a glitch> Thanks Again wildblue and MiiNiPaa!!
Last edited on
Topic archived. No new replies allowed.