So I'm doing a project and having to write a program. I've written it mostly and I'm really just asking for some peer review. I don't find any problems with it but just want to know if it looks good enough to turn in?
Another thing, since I have 1. done I'm doing the extra credit on 2. I'm thinking about how to do it. Please let me know if I'm on the write track or not.
Would I cin liters and miles for car1, cin liters and miles for car2. then output results for both. Now for announcing which car has the best fuel efficiency would using if else be the right way to display that?
I'm working on this myself but I would just like for someone to double check my work if you don't mind.
1. A liter is 0.264179 gallons. Write a program that will read in the number
of liters of gasoline consumed by the user’s car and the number of miles
traveled by the car and will then output the number of miles per gallon the
car delivered. Your program should allow the user to repeat this calculation
as often as the user wishes. Define a function to compute the number of
miles per gallon. Your program should use a globally defined constant for
the number of liters per gallon.
2. Modify your program from Practice Program 1 so that it will take input
data for two cars and output the number of miles per gallon delivered by
each car. Your program will also announce which car has the best fuel efficiency
(highest number of miles per gallon).
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>
using namespace std;
const double GALLONS_PER_LITER = 0.264179;
double MilesPerGallon(double miles, double gallons);
int main() {
int liters;
double miles;
double MPG;
char repeat;
do
{
cout << "Please input how many liters of gasoline is in your vehicle: ";
cin >> liters;
cout << "Please input the distance in miles you traveled in your vehicle: ";
cin >> miles;
MPG = miles / (liters * GALLONS_PER_LITER);
//display results
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout << "Consuming " << liters << " liters of fuel to travel "
<< miles << " miles, means you are getting "
<< MilesPerGallon(miles, (liters * GALLONS_PER_LITER))
<< " miles per gallon." << endl;
cout << "Your vehicle's MPG is: " << MPG << endl;
cout << "Would you like to repeat the process?\n";
cout << "Please enter: [Y/N]\n";
cin >> repeat;
} while (repeat == 'Y' || repeat == 'y');
system("pause");
return 0;
}
double MilesPerGallon(double miles, double gallons) {
return (miles / gallons);
}
|