Need Assistance fixing this program

Three cars drive a 500 mile route. Write a program that inputs the car make and the number of gallons of fuel used by each car. After called a calcMPG() function once for each car, have main determine and display which car was the most fuel efficient and how many miles per gallon it got. The calcMPG () function should be passed the distance and gallons of gas used as arguments, and should return the miles per gallon obtained.
----------------------------------------------------
Sample Results:

This program will determine which of 3 cars is the most fuel efficient



Please enter the make of car 1: Chevy Camaro

How many gallons of gas did this car use? 20.33



Please enter the make of car 2: Jeep Wrangler

How many gallons of gas did this car use? 19.2



Please enter the make of car 3: Toyota Camry

How many gallons of gas did this car use? 17.82



The most fuel-efficient car was the Toyota Camry.

It got 28.06 miles per gallon.
--------------------------------------------

I'm pretty beginner level at this C++, and I tried to set up this program but I'm completely lost. Here's what I got.

#include <iostream>
using namespace std;
int main()
{
// Variables
float name_of_car,
num_of_gal_used,
MPG;

// Ask user for the name of the car
cout << endl;
cout << "Enter the name of the car: ";
cin >> name_of_car;

// Ask for num of gallons that the car has used
cout << "Enter a number of gallons the car used \n";
cout << "driven on a full tank of gas: ";
cin >> num_of_gal_used;

// Calculate num of miles the car can drive per gallon
MPG = full_tank_miles / num_of_gallons;

// Display calculation
cout << "The car gets " << MPG << " miles per gallon.";
cout << '\n' << endl;
return 0;
}
Consider:

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
#include <iostream>
#include <iomanip>
#include <string>

struct Cars {
	std::string name;
	double gallons {};
};

double calcMPG(double dist, double gallons)
{
	return dist / gallons;
}

int main()
{
	const size_t NoCars {3};
	const double miles {500};

	Cars cars[NoCars] {};

	for (size_t c = 0; c < NoCars; ++c) {
		std::cout << "Please enter the make of car " << c + 1 << " : ";
		std::getline(std::cin >> std::ws, cars[c].name);

		std::cout << "How many gallons of gas did this car use? ";
		std::cin >> cars[c].gallons;
	}

	double bestmpg {};
	size_t bestcar {};

	for (size_t c = 0; c < NoCars; ++c)
		if (const auto mpg {calcMPG(miles, cars[c].gallons)}; mpg > bestmpg) {
			bestcar = c;
			bestmpg = mpg;
		}

	std::cout << "The most fuel-efficient car was the " << cars[bestcar].name << ".\n";
	std::cout << "It got " << std::fixed << std::setprecision(2) << bestmpg << " miles per gallon.\n";
}

Read the assignment carefully. Every sentence dictates something about the code.

In the sample output, how did the code get 28.06 MPG? What's the math there? I'm asking because you've missed something important in your code.

Make your code match the sample output exactly.

Copy the assignment into a .cpp file and turn it into a comment. Now move the comments around to where the program does what the comment calls for. Doing this makes it practically impossible to miss something.
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
#include <iostream>
#include <string>
using std::cstring;

// The calcMPG () function should be passed the distance and gallons
// of gas used as arguments, and should return the miles per gallon
// obtained.
double
calcMPG(double distance, double gallons)
{
    return distance / gallons;
}


int main()
{
    // Write a program that inputs the car make and the number of
    // gallons of fuel used by each car.
    for (int i=0; i<3; ++i) {
	// Input car make and number of gallons

	// After called a calcMPG() function once for each car, have main
	// determine .. which car was the most fuel efficient

	// Three cars drive a 500 mile route.
    }

    // ... and display which car was the most fuel efficient and how
    // many miles per gallon it got.
}


Next figure out what data you need. I've also flushed out the algorithm a little:
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
#include <iostream>
#include <string>
using std::cstring;

// The calcMPG () function should be passed the distance and gallons
// of gas used as arguments, and should return the miles per gallon
// obtained.
double
calcMPG(double distance, double gallons)
{
    return distance / gallons;
}


int main()
{
    // Write a program that inputs the car make and the number of
    // gallons of fuel used by each car.
    string bestCarName;		// name of most efficient car
    double bestCarMPG = -1;	// MPG of most efficient car.

    for (int i=0; i<3; ++i) {
	string carName;		// name of current car
	double carMPG;		// MPG of current car
	double carMiles;	// miles driven by current car
	
	// Input car make and number of gallons

	// After called a calcMPG() function once for each car, have main
	// determine .. which car was the most fuel efficient

	// Three cars drive a 500 mile route.

	// If the current car's mileage is better than the best car,
	// then set bestCarName and bestCarMPG.
    }

    // ... and display which car was the most fuel efficient and how
    // many miles per gallon it got.

    // Display bestCarName and bestCarMPG
}



Topic archived. No new replies allowed.