Function Call

I am trying to write a program that will calculate charges by calling a function to the main branch but I keep Coming back with 0 no mater what I input. Here is my program;

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

// Fuction header
double billingAmount(double hourlyRate, double consultingTime, string lowIncome);

/// Main Program 

int main()
{
	double a = 0, b = 0;
	string c;
	double r = 0;

	r = billingAmount(a, b, c);

        cout << "Enter Hourly Rate :";
	cin >> a;
	cout << endl;

	cout << "Enter Consulting Time In Minutes :";
	cin >> b;
	cout << endl;


	cout << "Is Client Low Income(Enter Y or N) :";
	cin >> c;
	cout << endl;

	cout << r;

	system("pause");
	return 0;

}

double billingAmount(double hourlyRate, double consultingTime, string lowIncome)
{
	double x=0;

//// Low Income determination 
	if (lowIncome == "Y" && consultingTime <= 30)
	{
		  x = 0;
	}
	
	else
	    if (lowIncome == "Y" && consultingTime > 30)
	    {
		 x = hourlyRate * .40 *((consultingTime - 30) / 60);
	    }
	    else
		if (lowIncome == "N" && consultingTime <= 20)
		{
		  x = 0;
		}
		else 
		    if (lowIncome == "N" && consultingTime <= 20) 
		{
			double x = hourlyRate * .70 * ((consultingTime - 20) / 60);
		}
	
	return x;
Last edited on
First, please use code tags in posts. See http://www.cplusplus.com/articles/jEywvCM9/
You can edit your post.

Now, your main():
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
int main()
{
  double a = 0, b = 0;
  string c;
  double r = 0;

  // at this point a==0, b==0, c==""
  r = billingAmount( a, b, c );
  // now you have already set r to what you will see in output

  // all this input has no effect on r:
  cout << "Enter Hourly Rate :";
  cin >> a;
  cout << endl;

  cout << "Enter Consulting Time In Minutes :";
  cin >> b;
  cout << endl;

  cout << "Is Client Low Income(Enter Y or N) :";
  cin >> c;
  cout << endl;

  cout << r;

  return 0;
}

Move the function call from line 8 to line 23. To after the input. You need to first get proper values, before calling a function.


This is different, from say a spreadsheet, where one can enter in cell A3 an equation =A1+A2 and later type values into cells A1 and A2, with immediate re-evaluation of the value of the dependent cell A3.
Thanks for the advise on the how to input the code in the Forum.
But i am still having the issue with it coming back as 0.
The conditions on line 56 and 61 are exactly the same.
Referring to your original post.

Line 63: You're declaring a new variable named x. This hides the variable x declared at line 42.
This second x (and the value it was set to) goes out of scope at line 64. The value of this calculation will never be returned.

Topic archived. No new replies allowed.