0

Good day

I am completing an assignment question and have come across a problem in writing the code that i can not figure out how to rectify.

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.

 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 code:

#include <iostream>
#include <cmath>
#include <string>

using namespace std;

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

}
//Takes the gender of driver and test for male and return additnal fee
bool 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 age, int experience, int gender, int marital)
{

    int Totalpremium = driversgroup(experience) + ageGroup(age) + 
    isMale(gender) + isAvailable(marital);

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


int main()
{
    int age;
    int experience;
    char gender;
    char marital;


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

 computePremium(age,experience,gender,marital);

}

Problems i am experiencing:

1) When the program runs and it asks me to put in the marital status if i input a lowercase letter it returns "Try again, invalid entry". How do i correctly name marital status so that it will allow me to enter both lowercase and uppercase letters?

2) It appears my value for my premium rate is not being calculated correctly. Any ideas on where my error could be?

Thank you

Geo
  • 31
  • 6
  • 8
    `if (marital == 'S' || 's' || 'D' || 'd'){` does not do what you think it does (hint: this will always evaluate to `true`). Also the correct thing to do would be to (learn to) debug your code (using a debugger). Further your `computePremium` function invokes *undefined behavior* (lack of `return` statement). You should probably start by reading [a good beginner's book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – UnholySheep Sep 25 '17 at 13:39
  • 1
    And use `-Wall` compiler flag and `-Werror`. – Yakk - Adam Nevraumont Sep 25 '17 at 13:43
  • 1
    You could use `std::toupper` or `std::tolower` to convert `marital` to either lower case or upper case before comparing. – Thomas Matthews Sep 25 '17 at 13:55

2 Answers2

0

I answer to your question because, unlike many other SO questions, you tried something:

  1. Your problem is here:if(marital != 'm' || 'M' || 'd' || 'D' || 's' || 'S' || 'l' || 'L') This always evaluates to true. You must check merital status against every possible solution like this: (if marital != 'S' && marital != 's' ...). Same problem for isAvailable(int marital) function. Furthermore, as Thomas Matthews said, you can use std::toupper or std::tolower to reduce the number of checks you do.

  2. One of your problems is here: bool isMale(char gender): if the expression is evaluated to true, it will return 1 not 10. You should change the function to int isMale(char gender). The other problem is at isAvailable function: if marital is not 'S', 's', 'D' or 'd' the function won't return a useful value.

Also, take UnholySheep advice and (learn to) debug your code and read a good beginner's book.

I recommend to think and test against various cases to fix the logical problems in your application. Example: what happens if the age/experience provided is a negative number? Is it the correct behavior?

Cristian Ionescu
  • 181
  • 3
  • 10
0

Try instead of bool isMale (char type), int isMale (int type) this should fix the problem with your calculation. also remember the Const Int which is equal to 850 that needs to be part of your calculation as that is the flat rate.

try :

if (experience >= 0 && experience <= 2)
{
    return 150;
}
else if and so on

int ageGroup(int age)

else if (age >= 46 && age <= 60 )


if (gender == 'M')
    cout << "Are you married?"
cin >> marital;

if (marital == 'M' && marital == 'm' && marital == 'L' && marital == 'l')
    cout << "No Extra charge" << endl;

Hope this helps a little

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
  • Thank you very much. Used all the help i was given and managed to get my program to execute perfectly – Geo Sep 25 '17 at 21:27