Problem with return value

My program is to display the values for someones gas and electric bill. It has an if-else for a bill to account for a baseline and over-baseline price for both the gas and electric. The function that computes the gas bill is correct but when the electric bill is computed and displayed it prints the same weird scientific notation value regardless of what the start and end electric values are. So what is wrong? Can you show or tell me b/c everything else if right except that.
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;
double price (double a, double b, double c, double d, double&, double& );



int main ()
{
	double start_gas, end_gas, gas_value;
	double start_electric, end_electric, electric_value;

	cout<<"Enter the starting value for gas:"<<endl;
	cin>>start_gas;
	cout<<endl;
	cout<<"Enter the ending value for gas:"<<endl;
	cin>>end_gas;
	cout<<endl;
	cout<<"Enter the starting value for electric:"<<endl;
	cin>>start_electric;
	cout<<endl;
	cout<<"Enter the ending value for electric:"<<endl;
	cin>>end_electric;
	cout<<endl<<endl;
	 
	price (start_gas, end_gas, start_electric, end_electric, gas_value, electric_value);
	
	cout<<gas_value<<endl;
	cout<<electric_value<<endl;
	
	system ("pause");
	return 0;
}
	
	double price (double start_gas, double end_gas, double start_electric, double end_electric, double& gas_value, double& electric_value) 
	{
		double gas, gas_value1, gas_value2;
		double electric, electric_value1, electric_value2;

		gas = (end_gas)-(start_gas);
		electric = (end_electric)-(start_electric);

		if (gas <= 31)
		{
			gas_value = gas*.504;
			
			return gas_value;
		}
		else 
		{
			gas_value1 = 31*.504;
			gas_value2 = (gas-31)*.824;
			gas_value = gas_value1+gas_value2;
			
			return gas_value;
		}
		//this is where the problem is
		if (electric <= 238.7)
		{
			electric_value = electric*.094;
			
			return electric_value;
		}
		else
		{
			electric_value1 = 238.7*.094;
			electric_value2 = (electric-238.7)*.133;
			electric_value = electric_value1+electric_value2;
			
			return electric_value;
		}
		
		
		
		
		
		
	}  
electric_value is never calculated and therefore you're printing an uninitialized value.

You return at either line 46 or line 54. You never reach line 56.

So how do I go about fixing that?
Are you saying the second if-else is never executed or executed properly?
Function price() passes gas_value and electric_value by reference. That means the function does not need to use a return statement for these values.

Unless you wanted some sort of combined total, there doesn't seem to be a need for a return statement at all, the function could have a return type of void and get rid of all the return statements, since these are preventing any code after the return was reached from being executed.
So how do I go about fixing that?
Are you saying the second if-else is never executed or executed properly?


The second if-else is never executed. A function can only return once, and when it returns, it exits. When you return the gas value, it exits, before the second if-else begins.

As Chervil says, your passing the results of the calculations back as function arguments anyway, so there's no need to return any values. And you're not using the return value from price() in main anyway.
Last edited on
Topic archived. No new replies allowed.