Need help with overloading functions

closed account (2vD2y60M)
This is my first time using overloading functions and im stuff on how i would set up what will be returned as well as making it so the program out puts which car has the best MPG.

This is suppose to convert the liters input by the user into gallons and gather the distance as well to find the MPG. The program needs to allow the user to run it as many times as they like. After all this is gathered then it tells the user which car has the best MPG.

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

double mpg(double liters, int miles);
//Returns the mpg of car 1 
double mpg(double liters, double miles);
//Returns the mpg of car 2

const double GALLONS_PER_LITER = 0.264179; //conversion of how many gallons are in a liters, global
char ans;

int main() {
	double liters, mpg;
	int miles;
		do {
	    
			cout << " How many liters of gas are in your tank: ";// asks user for liters in tank
			cin >> liters;

			cout << " How many miles did you travel: "; // asks user for distance traveled
			cin >> miles;

			mpg = miles / (liters * GALLONS_PER_LITER); // function to find MPG

			cout << " Your car's MPG is " << mpg << ".\n"; // outputing the users MPG

		    cout << " Would you like to run the program again?";//Allowing user the option to run again
                    cin >> ans;

		 }while (ans == 'y' || ans == 'Y');
	system("pause");
	return 0;
}

double mpg(double liters, double miles) {
	double rate, mpg;

	cout << " How many liters of has are in your tank: ";
	cin >> liters;

	cout << " How many miles did you travel: ";
	cin >> miles;

	mpg = miles / (liters * GALLONS_PER_LITER);
	


}
Last edited on
This isnt overloading operators... Last time I remember, overloading functions are like: operator== operator<< inside of classes.
What you need to do is just google the conversion ratio of liter to gallon, and you are also not defining the variable ans in your code.
closed account (2vD2y60M)
I already have the conversion ratio for liters to gallons
closed account (2vD2y60M)
http://www.learncpp.com/cpp-tutorial/76-function-overloading/
This is what i am doing
Topic archived. No new replies allowed.