Learning functions and need some help...

Hello. I am getting into functions in my computer science class and have an assignment that requires one. I sort of understand them, but obviously not TOO well. I have messed around using functions in a side project I did for myself, but it was simple arithmetic problems. However, this problem is confusing me and I just can't understand why it won't work. If someone may be kind to further explain and point me in the right direction, I would appreciate it so much.


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
#include <iostream>

using namespace std;

//signature 
double totalBill(double payRate, double consultTime, double totalIncome);

int main()
{
	double totalIncome, consultTime, payRate;
	totalIncome = 0;
	consultTime = 0;
	payRate = 0;

	cout << "Hello. We, from J&J accounting firm, will provide you with the total bill due." << endl;
	cout << "Enter the income: ";
	cin >> totalIncome;
	cout << endl;

	cout << "Enter total duration time in minutes: ";
	cin >> consultTime;
	cout << endl;

	cout << "Enter the hourly charge rate: ";
	cin >> payRate;
	cout << endl;
	cout << totalBill(totalIncome, consultTime, payRate);
	return 0;
}

//function to calculate billing amount
double totalBill(double payRate, double consultTime, double totalIncome){
	
	//variables used
	double total = 0;
	payRate = 0;
	consultTime = 0;
	totalIncome = 0;


	if (totalIncome <= 25000){
		//check duration
		if (consultTime <= 30){
			cout << "There is no charge2." << endl;
			return 0;
		}//end if

		else {
			total = payRate * 0.40 * ((consultTime - 30) / 60);
			cout << "Your total is = ";
			return total;
		}//end else

	}//end if for low income check

	else if(totalIncome >= 25000){

		if (consultTime <= 20){
			cout << "There is no charge1." << endl;
			return 0;
		}

		else {
			total = payRate * 0.70 * ((consultTime - 30) / 60);
			cout << "Your total is = ";
			return total;
		}//end else
	}//end else
}


I put "charge2" and "charge1" because I wanted to see which line the program was outputting.
Well, no worries- your mistake is not too awful of one. Line 27:
cout << totalBill(totalIncome, consultTime, payRate);
But the function is:
double totalBill(double payRate, double consultTime, double totalIncome);

I believe line 27 might be a bit... backwards?

Last edited on
1
2
3
4
5
6
7
double totalBill(double payRate, double consultTime, double totalIncome){
	
	//variables used
	double total = 0;
	payRate = 0;
	consultTime = 0;
	totalIncome = 0;


Why are you passing in these values and then setting them all to zero?

Instead just:

1
2
3
4
double totalBill(double payRate, double consultTime, double totalIncome){

	//variables used
	double total = 0;


Also you should rethink the logic in the totalBill function and do a single return at the end.
Last edited on
Thank you so much Ispil! That did it. Now I have a question, after this problem I encountered where I had the variables backwards, I am assuming when I call a function, the variables have to be in the same order as the ones in the function?

Mobutus, I just have the habit to initialize the variables I use. If I am thinking along the same lines as you, I was thinking I could use
1
2
3
4
5
6
7
8
	if (totalIncome <= 25000 && consultTime <= 30){
		cout << "There is no charge." << endl;
	}
	else {
			total = payRate * 0.40 * ((consultTime - 30) / 60);
			cout << "Your total is = ";
			return total;
		}//end else 


Was that what you were referring to? Also, since I deleted return 0; from the if statement, I get some weird error after the program runs.

How can I get "There is no charge" to return without return 0; ? With return 0;, it would return a 0...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
double totalBill(double payRate, double consultTime, double totalIncome){
	
	//variables used
	double total = 0;

	if (totalIncome <= 25000 && consultTime >= 30){
			total = payRate * 0.40 * ((consultTime - 30) / 60);
			cout << "Your total is = ";
			return total;
		}//end low income total
	else if(totalIncome >= 25000 && consultTime >= 20){
		total = payRate * 0.70 * ((consultTime - 20) / 60);
		cout << "Your total is = ";
		return total;
	}
	else{
		cout << "There is no charge.";
		return total;
	}
}


My updated code.
Last edited on
You don't have to initialize payRate, consultTime or totalIncome they come into your function with a value already.

Here is your function using only one return statement.
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
//function to calculate billing amount
double totalBill(double payRate, double consultTime, double totalIncome){

	//variables used
	double total = 0;

	if (totalIncome <= 25000){
		//check duration
		if (consultTime <= 30)
			cout << "There is no charge2." << endl;
		else {
			total = payRate * 0.40 * ((consultTime - 30) / 60);
			cout << "Your total is = ";
		}//end else
	}//end if for low income check

	else{
		if (consultTime <= 20)
			cout << "There is no charge1." << endl;
		else {
			total = payRate * 0.70 * ((consultTime - 30) / 60);
			cout << "Your total is = ";
		}//end else
	}//end else
	return total;
}
Last edited on
Topic archived. No new replies allowed.