Insuring a driver

I'm having an issue with my declared function. My assignment is:

Write a program that can be used by an insurance company to indicate whether or not they
are prepared to insure a given driver, and if they are to indicate what their annual policy
payment should be (based on a fixed rate of £300 plus an additional loading).
The program should ask the user for the driver’s age (in years), the number of motoring
convictions that they have and the cubic capacity of their vehicle’s engine. The program will
then decide whether or not the company is prepared to insure the driver based on the
following rules:
• A driver under the age of 20 or over the age of 70 will not be ensured
• A driver under the age of 25 with no convictions will only be ensured if they have a

vehicle with an engine cubic capacity less than 2000.
• A driver who is under the age of 25 and has no more than 2 convictions wil be insured if
their vehicle has a cubic capacity less than 1300
• A driver with more than 4 convictions will not be ensured.

The company has a basic policy rate of £300. Any additional loading to be added to this is
based on the following rules:
• An extra £10 is added for every year that the driver is under the age of 30
• An extra £50 is added for every motoring conviction the driver has
• An extra £100 is charged for any vehicle with an engine capacity greater than 2000cc

Example: if the input to the program represents a customer aged 23 with 2 convictions and
a 1250cc vehicle, the program should output the following:

This driver can be insured with a premium cost of £470.

However if the input to the program represents a customer aged 24 with one conviction and
a 2500cc vehicle, the program should ouput the following:

This driver cannot be insured.

I have commented it out (calc_else), which is where I believe the error lies.


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
 #include<string>
#include<iostream>
using namespace std;
const int BASIC_POLICY_RATE = 300;
void insurance(int& age, int& engine_size, int& no_convictions, string& message);
//string calc_else(int age,  int engine_size, int no_convictions, string message);

int main()
{
	int age, engine_size, no_convictions, price;
	string message;

	cout << "Enter your age: ";
	cin >> age;
	cout << "Enter the number of convictions you currently have: ";
	cin >> no_convictions;
	cout << "Enter the cubic size of your engine: ";
	cin >> engine_size;

	insurance(age, engine_size, no_convictions, message);

	cout << endl;
	cout << message << endl;
	
	
	system("PAUSE");
	return 0;
}

void insurance(int& age, int& engine_size, int& no_convictions, string& message)
{   int rule1, rule2, price;
	if (age < 20 || age > 70)
		{message = "This driver cannot be insured";}

	else if (age < 25 && no_convictions == 0 && engine_size < 2000)
		{rule1 = (30-age)*10;
		 price = BASIC_POLICY_RATE + rule1;
		 message = "This driver can be insured with a premimum cost of £" + price;
		}
	else if (age < 25 && no_convictions <= 2 && engine_size < 1300)
		{rule1 = (30-age)*10;
		 rule2 = no_convictions*50;
		 price = BASIC_POLICY_RATE + (rule1 + rule2);
		 message = "This driver can be insured with a premimum cost of £" + price;}

	else if (no_convictions > 4)
		{message = "This driver cannot be insured";}
	else
		{//message = calc_else(age, engine_size, no_convictions, message);
	}
}

/*string calc_else(int age,  int engine_size, int no_convictions, string message)
	{
	 int rule1, rule2, rule3;
	 int price;

	 // £10.00 added for every year driver is under 30
	 if (age <= 30)
	 {rule1 = (30 - age)*10;}
	 else
	 {rule1 = 0;}
	 // £50.00 added for every motoring conviction
	 rule2 = no_convictions * 50;
	 // £100 if engine size is greater than 2000cc
	 if (engine_size >= 2000)
	 {rule3 = 100;}
	 else
	 {rule3 = 0;}

	 price = BASIC_POLICY_RATE + (rule1 + rule2 + rule3);
	 message = "This driver can be insured with a premium cost of £" + price;
	}
	*/


in your calc_else() function you don't actually do anything with 'message' once it's passed in.
You don't also return anything from this function, which should make it not compile.
Last edited on
Oh wait I forgot to add return type for my function. Thanks.
After modifying my sub-routines and fiddling with my if statements I got it work with the supplied test data in my assignment question.


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<string>
#include<iostream>
using namespace std;
const int BASIC_POLICY_RATE = 300;

void insurance(int age, int engine_size, int no_convictions, int& price, string& final_output);
void calc_else(int age,  int engine_size, int no_convictions, int price, string& final_output);

int main()
{
	int age(0), engine_size(0), no_convictions(0), price(0); string final_output;

	cout << "Enter your age: ";
	cin >> age;
	cout << "Enter the number of convictions you currently have: ";
	cin >> no_convictions;
	cout << "Enter the cubic size of your engine: ";
	cin >> engine_size;

	insurance(age, engine_size, no_convictions, price, final_output);
	cout << endl;
	if (price == 0)
		cout << final_output << " " << endl << endl;
	else
		cout << final_output << " " <<  price << endl << endl;
	
	system("PAUSE");
	return 0;
}

void insurance(int age, int engine_size, int no_convictions, int& price, string& final_output)
{   int rule1, rule2, price_else; string final_output_else;

	if (age >= 20 || age <= 70)
		if (no_convictions <= 4)
			if (age < 25)
				if (no_convictions == 0 && engine_size < 2000)
					 {rule1 = (30-age)*10;
					  price = BASIC_POLICY_RATE + rule1;
					  final_output = "This driver can be insured with a premimum cost of GBP";}
			
				else if (no_convictions <= 2 && engine_size < 1300)
					{rule1 = (30-age)*10;
					 rule2 = no_convictions*50;
					 price = BASIC_POLICY_RATE + (rule1 + rule2);
					 final_output = "This driver can be insured with a premimum cost of GBP";}			
				else
					{final_output = "This driver cannot be insured";}
			else 
				{calc_else(age, engine_size, no_convictions, price_else, final_output_else);
				final_output = final_output_else;
				price = price_else;}
		else 
			{final_output = "This driver cannot be insured";}
	else
			{final_output = "This driver cannot be insured";}
}

void calc_else(int age,  int engine_size, int no_convictions, int price_else, string& final_output_else)
	{
	 int rule1, rule2, rule3;
	 // £10.00 added for every year driver is under 30
	 if (age <= 30)
	 {rule1 = (30 - age)*10;}
	 else
	 {rule1 = 0;}
	 // £50.00 added for every motoring conviction
	 rule2 = no_convictions * 50;
	 // £100 if engine size is greater than 2000cc
	 if (engine_size >= 2000)
	 {rule3 = 100;}
	 else
	 {rule3 = 0;}

	 price_else = BASIC_POLICY_RATE + (rule1 + rule2 + rule3);
	 final_output_else = "This driver can be insured with a premium cost of GBP";
	}
Topic archived. No new replies allowed.