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;
}
}
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: usingnamespace 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.
#include <iostream>
usingnamespace std; // <--- Best not to use.
double calcMPG(double distInMiles, double gallon)
{
return distInMiles / gallon; //return miles per gallon
}
int main()
{
// Establish variables
constexprdouble DIST_IN_MILES{ 500.0 }; // <--- Changed.
constexprunsignedint 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.