Insurance Premium Calculator

C++
I'm tasked with an assignment to create an insurance premium calculator. I have built my code so far up to this point
However, my code isn't working as intended. These are the requirements for the program:

Req 1: The base monthly cost of insuring a car is $50.00 -- this is the starting cost, we will add to this cost as we collect more information (see the rest of the requirements)

Req 2: Your program should get the age of the driver from the user.
If the driver is under the age of 25, the monthly premium goes up $100;
otherwise, If the driver is under the age of 35, the monthly premium goes up $20.

For example, for a 23-year-old driver, you should add $100 to the base premium. For a 28-year-old driver, you should add only $20 to the base premium. Any driver over the age of 35, does not get any additional premium due to age.
Req 3: Program should get the number of accidents in the last 5 years from the user.
Up to one accident is forgiven under accident forgiveness policy.
If the number of accidents is less than 3, then you add $40 per accident.
If the number of accidents is more than or equal to 3, you add $60 per accident to the monthly premium.

If driver had more than 4 accidents in the past 5 years, we cannot provide insurance (let the user know with a message).
Number of Accidents: Accident Surcharge
1 Accident None = Accident Forgiveness, no additional charge
2 Accidents $40 per accident = $80 additional charge
3 Accidents $60 per accident = $180 additional charge
4 Accidents $60 per accident = $240 additional charge
5 Accidents Can not insure
Use the following test cases to make sure that your program is functioning properly.
Based on the above 3 requirements, compute and output the monthly premium for the driver.
Test Case 1: 24 years old driver with no accident should output $150.00.
Test Case 2: 24 years old driver with 3 accidents should output $330.00.
Test Case 3: 26 years old driver with no accidents should output $70.00.
Test Case 4: 50 years old driver with 2 accidents should output $130.00.
What am I doing wrong? I've been trying to figure it out myself, but I'm running out of time.

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
 #include<iostream>
using namespace std;

int main()

{
	int base = 50;
	int premium = base;
	int age;
	int NumAcc;
	double total = 0;
	int mainChoice;
	const int choiceOne = 1;
	const int choiceTwo = 2;
	
	do
	{
		//Display the Menu
		cout << "\n\t\t>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n\n";
		cout << "\t\t*Insurance Premium Assistant V1.8*" << endl;
		cout << "\n\t\t<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n\n"<<endl;
		cout << "1.Calculate the premium for a new applicant"<<endl;
		cout << "2.Exit the program"<<endl;
		cout << "Enter you choice :";
		cin >> mainChoice;
		switch (mainChoice)
		{
		case 1:
			cout << "Enter the age of the applicant: ";
			cin >> age;
			if (age<25)
			{
				premium += 100;
				
			}
			else if (age<35||age>25)
			{
				premium += 20;
			}

			cout << "Number of accidents: ";
			cin >> NumAcc;
			if (NumAcc==1)
			{
				cout << "The total premium: " << premium << endl;
				break;
			}
			else if (NumAcc<3)
			{
				int prem1 = premium + (40 * NumAcc);
				cout << "The total premium: " << prem1 << endl;
				break;
			}
			else if (NumAcc == 3 || NumAcc <= 4)
			{
				int prem2 = premium + (60 * NumAcc);
				cout << "The total premium: " << prem2 << endl;
				break;
			}

			else if (NumAcc > 4)
			{
				cout << "Sorry, we cannot provide insurance to this client"<< endl;
			}
	
		}

	}
	
	while (mainChoice!=0);
		return 0;


	system("PAUSE");
	return 0;
}
A few things look like they need fixing.

First, the declaration at line 8:
 
    int premium = base;
That looks ok. However ... after executing
line 33 premium += 100; or line 38 premium += 20; that means the premium for all subsequent customers is also increased.

What you need to do is to start afresh with each customer.
I suggest you move line 8. Put it inside the while loop, say at line 18, or anywhere before you start to get the inputs and process the details for the customer.

The other thing is this. Several of the if statements look incorrect.

 
    if (age<25)
Good - this one is ok.

What about this one?
 
    else if (age<35||age>25)

It will certainly catch the case where age is 26. But what about age 50? since 50 is greater than 25, it will satisfy the condition age>25 because the logical OR operator is used, which means the whole condition is true if any of the individual conditions is true. It should just be
 
    else if (age < 35)




The line if (NumAcc==1) really should include 0 or 1 accidents, hence it would be better as if (NumAcc <= 1)

 
    else if (NumAcc == 3 || NumAcc <= 4)
- this is more or less correct, but it could be simplified to just else if (NumAcc <= 4) .

The last one is overdone too,
 
    else if (NumAcc > 4)
by the time this line is reached, we already know that NumAcc must be greater than 4, otherwise it would have been caught by one of the previous if-statements. Hence all you need there is simply: else


Hope this helps.
Last edited on
Topic archived. No new replies allowed.