C++ programming challenge

7. Most Fuel Efficient Car

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 calling 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 driven and the gallons of gas used as arguments, and should return the miles per gallon obtained.

my issue is when i enter the carmake it then displays everything else and the program has ended, any ideas what i'm doing wrong


#include <iostream>
using namespace std;
float calcMPG(float distInMiles, float gallon)
{
return distInMiles / gallon; //return miles per gallon
}

int main()
{
// Establish variables
float distInMiles = 500;
float effi_c1, effi_c2, effi_c3, gallons;
int carMake;

// Ask for car make and amount of gallons used
cout << " What is the make of the car? ";
cin >> carMake;
cout << " How many gallons of gas did this car use? ";
cin >> gallons;
effi_c1 = calcMPG(distInMiles, gallons);

cout << " What is the make of the car? ";
cin >> carMake;
cout << " How many gallons of gas did this car use? ";
cin >> gallons;
effi_c2 = calcMPG(distInMiles, gallons);

cout << " What is the make of this car? ";
cin >> carMake;
cout << " How many gallons of gas did this car use? ";
cin >> gallons;
effi_c3 = calcMPG(distInMiles, gallons);


// Determine which car is most efficent
if (effi_c1 > effi_c2)
{
if (effi_c1 > effi_c3)
{
cout << "car 1 is most efficient.\n";
cout << "efficiency (in miles per gallons) : " << effi_c1;
}
else
{
cout << "car 3 is most efficient.\n";
cout << "efficiency (in miles per gallons) : " << effi_c3;
}
}
else
{
if (effi_c2 > effi_c3)
{
cout << "car 2 is most efficient.\n";
cout << "efficiency (in miles per gallons) : " << effi_c2;
}
else
{
cout << "car 3 is most efficient.\n";
cout << "efficiency (in miles per gallons) : " << effi_c3;
}
}

return 0;
}
Hello jtherudy96,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



You asked what you are doing wrong:
using namespace std; // <--- Best not to use.

Prefer using "double"s over "float"s.

When you get to these 2 lines:
1
2
cout << " What is the make of the car? ";
cin >> carMake;

What do you enter for the car make? A string or a number? Most people would be likely to enter a string.

That is what I see initially. I have not had a chance to go over the entire program yet.

Andy
Hello jtherudy96,

A little revising of your code:
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
#include <iostream>

using namespace std;  // <--- Best not to use.

double calcMPG(double distInMiles, double gallon)
{
    return distInMiles / gallon; //return miles per gallon
}

int main()
{
    // Establish variables
    constexpr double DIST_IN_MILES{ 500.0 };  // <--- Changed.
    constexpr unsigned int MAXSIZE{ 4 };      // <--- Added.

    double effi[MAXSIZE]{};                   // <--- Added.
    double effi_c1{}, effi_c2{}, effi_c3{}, gallons{};
    int carMake{};  // <--- Defined and asked for, but never used.

    // Ask for car make and amount of gallons used
    cout << "\n What is the make of the car? ";
    cin >> carMake;

    cout << " How many gallons of gas did this car use? ";
    cin >> gallons;

    effi_c1 = calcMPG(DIST_IN_MILES, gallons);

Note: A few blank lines makes the code easier to read.

For the most part this much is good. Although you ask the same thing 3 times. This could easily be done in a for loop. That is what you can use line 16 for. Although 3 variables are small enough there is no need to change what you have. Whether you use a for loop and array at 3, 4 or 5 is your choice and how much you want to type.

I thought of using a struct and an array of structs, but since you only have 1 variable to keep track of there would be little to no advantage in using a struct.

With what little testing I did the section "// Determine which car is most efficient ", spelling, appears to work. Although you may get some more efficient examples from others.

Andy
Something like for C++17:

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

float calcMPG(float distInMiles, float gallon)
{
	return distInMiles / gallon; //return miles per gallon
}

int main()
{
	// Establish variables
	const float distInMiles {500};
	const size_t noCars {3};
	float maxeff {};
	size_t maxcar {};

	std::string carName[noCars];

	for (size_t c = 0; c < noCars; ++c) {
		cout << "What is the make of car " << c + 1 << " : ";
		getline(cin >> ws, carName[c]);

		float gallons {};
		cout << "How many gallons of gas did car " << c + 1 << " use: ";
		cin >> gallons;

		if (const auto mpg {calcMPG(distInMiles, gallons)}; mpg > maxeff) {
			maxeff = mpg;
			maxcar = c;
		}
	}

	cout << "car " << maxcar + 1 << " [" << carName[maxcar] << "] is most efficient.\n";
	cout << "efficiency (in miles per gallons) : " << maxeff << '\n';
}

thank you all for the help! i really appreciate it
Topic archived. No new replies allowed.