Not understanding functions

Hi all,

I'm new here to the forums but I've taken a look at the references and tutorials in the past. I was hoping to avoid going to the forums for help but I'm overdue on two assignments and the semester is almost over. I just need help completing these two assignments so I can clean them off my plate, so to speak.

As to the reason they're overdue - I was out with the flu a month ago for two weeks. I haven't been able to catch up both homework wise and worse, information wise. I'm not understanding what I've missed, which thus far has been functions and iostream (file input/output). For this topic I will focus on project 5.

Project: Write a function that reads all information from the user, and a function (or functions) that calculate insurance charge rates based on this information.

Indepth details: Problem Description: The HealthCare Insurance Agency determines auto insurance rates based in the driver's age, ticket history, and the value of the car. The base rate is 6% of the car value.

Male drivers under the age of 25 pay an extra 17%, and female drivers under the age of 21 pay an extra 4% of the base charge. A driver with more than three tickets pays an extra $100.

Write a function that reads all information from the user, and a function/functions calculates insurance charge rates based on this information. To test these functions write a program that uses these function to calculate insurance rates for information inputted by the user. Output answers in decimal form to two places.

Your program should allow the user to repeat this calculation as often as the user wishes

I'd like to add something before showing the code. Previously ask a friend for assistance but what he showed me is a little over my head. It doesn't help that I am not fully understanding functions. I suppose my main issue with understanding functions is their order in the logic tree.

This is the code line my friend suggested: int percent = 0.06 + (age <25 && g=="M" ? 0.17 : 0) + (age <21 && g=="F" ? 0.04 : 0);

He explained that it's a condensed version of a true-false statement. However, I am not sure where I would put this in the coding itself so I will put it to the side for now. I'd rather not use something I'm not fully understanding and learn about this first.

The Question: What sort of function would I use to call on multiple pieces of information?

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

// Functions


//Main body
int main()
{

// Variables
	int age,
	int car,
	int ticket,
	char g, //g is for gender
	char r, //r is for run
	float gender_rate,
	float rate;

	//Gather information

	cout << "Enter the value of the car: " << endl;
	cin >> car;
	cout << "Enter the age of the driver: " << endl;
	cin >> age;
	cout << "Enter the sex of the driver (M or F): " << endl;
	cin >> g;
	cout << "Enter the number of tickets on the drivers record: " << endl;
	cin >> ticket;

	//gender
	while(g=='m'||g=='M')
		if (g<25)
			gender_rate = rate + 17/100;
		else (g = 0);

	while(g=='f'||g=='F')
		if (g<21)
			gender_rate = rate + 4/100;
	else (g = 0);

	//ticket
	if (ticket>3)
		ticket = 100;
	else
		ticket = 0;
	

	//calculations
	int base = car * 60 / 100;

	rate = base + ticket;


	//Display results
	cout << "The insurance rate is " << rate;

	//run program again?
	cout <<"Would you like to run the program again (Y or N)?"<<endl;
	cin>>r;

	while(r=='y'||r=='Y');
}


TL;DR: I need help understanding functions. I don't want this code solved for me, I want to know where I'm going wrong and what I can do to improve.

Thanks,

-- DMW

EDIT - To try and give a better and more concise question.
EDIT2 - Changed title to fit question better
Last edited on
1) Read this:
http://www.programming4beginners.com/tutorial/functions/functions
(and read the sub-chapters)

Then this:
http://www.cplusplus.com/doc/tutorial/functions/

2) The statement in line 63 will not have the desired effect.

Read this:
http://www.programming4beginners.com/tutorial/chapter13/while-and-do-while-loops
(and read the next page, too)

Edit: Also note, when writing a function, try to make it to just calculate, not to do input/output. I.e. avoid cin/cout inside functions.
Last edited on
you know all that stuff inside the
1
2
3
4
5
int main() 
{
//stuff
return 0;
}


functions are just the same things. the 'main()' body is actually a function, as noted by the () parentheses.

the only somewhat confusing part is deciding which type of function you want. the most common is either int or void. if you use an int, or char, or string function, it must return an integer, character, or string literal.

but it doesn't sound like you are really asking about functions.

you can see how i take the output and input at the beginning of your code by declaring the function at the top (before int main()) and then putting the full function with body at the bottom.



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
83
84
85
86
87
88
89
90
91
  // Include Section 
#include <iostream>
using namespace std;

// Functions
void user_input_car(int car); // void returns nothing itself

int user_input_age(int age_var); //age_var is just a place holder, you could name it anything

//Main body
int main()
{

// Variables
	int age,
	int car,
	int ticket,
	int car_var;
	char g, //g is for gender
	char r, //r is for run
	float gender_rate,
	float rate;

	//Gather information
	
	user_input_car(car);

	user_input_age(age);

	cout << "Enter the sex of the driver (M or F): " << endl;
	cin >> g;
	cout << "Enter the number of tickets on the drivers record: " << endl;
	cin >> ticket;

	
	//gender
	while(g=='m'||g=='M')
		if (g<25)
			gender_rate = rate + 17/100;
		else (g = 0);

	while(g=='f'||g=='F')
		if (g<21)
			gender_rate = rate + 4/100;
	else (g = 0);

	//ticket
	if (ticket>3)
		ticket = 100;
	else
		ticket = 0;
	

	//calculations
	int base = car * 60 / 100;

	rate = base + ticket;


	//Display results
	cout << "The insurance rate is " << rate;

	//run program again?
	cout <<"Would you like to run the program again (Y or N)?"<<endl;
	cin>>r;

	while(r=='y'||r=='Y');
}


void user_input_car(int car)
{
	cout << "Enter the value of the car: " << endl;
	cin >> car;
	
	//this will not work, just showing you, void is not used to return anything.
	//there are ways to make it work though.
	// so you really need an int here, 
	// with a return car;  statement at the end
	// remember functions can return any other type as well.

}

int user_input_age(int age_var)
{
	cout << "Enter the age of the driver: " << endl;
	cin >> age_var; //age_var == age, even though its named differently here.

	return age_var;

}

Some problems with your original code:

line 34: Why are you comparing g to 25? Did you mean age?

Line 33: Why is this a while loop? This should be a simple if statement.

Line 36: Why are you setting g to 0? The else is not needed.

line 42: ditto while.

line 45: ditto else.

Line 35,40: What is the point of calculating gender_rate? You never use it.

Line 63: If r is 'y', that loop (which does nothing) will never terminate.
Last edited on
Topic archived. No new replies allowed.