Calling a Function help

closed account (2vD2y60M)
I need help making the program find which car has the highest mpg and also when i run this program it only asks for the input for the first car not the second...

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

double mpg_calc(double liters, int miles);
//Returns the mpg of car 1 
double mpg_calc(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, mpg1;
	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;

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

			cout << " Your car's MPG is " << mpg1 << ".\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_calc(double liters, double miles) {
	double rate, mpg2;


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

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

	mpg2 = miles / (liters * GALLONS_PER_LITER);

	return mpg2;
	


}

Last edited on
when i run this program it only asks for the input for the first car not the second...
Works for me

which car has the highest mpg
Introduce a new variable (like highest_mpg) and set it to 0.
After line 23:
1
2
if(mpg1 > highest_mpg)
  highest_mpg = mpg1;
Topic archived. No new replies allowed.