Understanding Funtions-

I'm writing a program that requires 3 functions and I don't quite understanding yet. I tried everything with this program but can't get it work.
*********************************************************************
Write a program that will call a function from main that will return the number of gallons of gas an individual buys, using a “copy of the value”. After this value is returned to main, call another function that will pass two arguments, the number of gallons of gas purchased and the number of liters. The number of gallons of gas you use as a argument must be passed as a copy of the value since no modifications of this value should occur in the function. The number of liters, in this function, will be calculated and returned by reference parameter. The number of liters is equivalent to the number of gallons used, multiplied by 1.7. After this value is returned to main, you are to call a third function that output both values – the number of gallons of gas purchased and its equivalent number of liters. Both of these are to be passed to the function by “copies of the value.”

You are to validate your input of the number of gallons purchased. Gallons cannot be a negative value. You are to request another value and stay in the loop until a valid response is given; do not end the program at this point.

The program must continue to run for three iterations.
***************************************************************************
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

#include <iostream>
using namespace std;

double gallon_bought(double);
double gallon_liter(double,double&);
double output_gallons_liters();

int main()
{
	double gallons;

	cout<<"Please enter total gallons bought: "<<endl;
	cin>>gallons;

	while (gallons <-1)
	{
		gallon_bought (gallons);
	}



	return 0;

}

double gallon_bought(double gallons)

{
	return gallons;

}

double gallon_liter(double gallons,double& liters)
{
	return liters=gallons*1.7;

}

double output_gallons_liters()
{
	cout<<"Total Gallons Bought: "<<gallon_bought<<endl;
	cout<<"Total Liters: "<<gallon_liter<<endl;

}


Last edited on
What is the problem you are having? Also, please use code tags.
I can't get it run right. Issues with my functions
http://www.cplusplus.com/doc/tutorial/functions/

While loop is kinda weird while we are less then -1 gallons? Also the function call here makes no since.

1
2
3
4
5
6
7
cout<<"Please enter total gallons bought: "<<endl;
cin>>gallons;

while (gallons <-1)
{
gallon_bought (gallons);
}


Use a boilerplate while loop. Get condition, validate, ask again if invalid.

1
2
3
4
5
6
7
8
	cout << "Please enter total gallons bought:  " << endl;
	cin >> gallons;

	while (gallons < 0)
	{
		cout << "Gallons cannot be less than 0. Enter again: " << endl;
		cin >> gallons;
	}


Also, why? You pass in gallons to get gallons back?

1
2
3
4
5
6
double gallon_bought(double gallons)

{
return gallons;

}


You are calling your functions wrong and this function could just be a void you are not going to be returning any value from this function.

1
2
3
4
5
6
double output_gallons_liters()
{
cout<<"Total Gallons Bought: "<<gallon_bought<<endl;
cout<<"Total Liters: "<<gallon_liter<<endl;

}


Pass in gallons, call you conversion function.

1
2
3
4
5
6
void output_gallons_liters(double gallons)
{
	cout << "Total Gallons Bought: " << gallons << endl;
	cout << "Total Liters: " << gallon_liter(gallons)  << endl;

}
Last edited on
Topic archived. No new replies allowed.