-
Homework Help Please
Question:
A liter is 0.264179 gallons. Write a program that will read in the number of liters of gasoline comsumed by the user's car and the number of miles traveled by the car, and will then output the number of miles per gallons 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 numbers of liters per gallon.
Code:
#include <iostream>
using namespace std;
int main()
{
const double LIT_GAL = .264179;
double miles;
double gallons;
double liters;
char again;
do
{
cout << "How many liters can your car hold?";
cin >> liters;
cout << "How many miles can your car drive without refilling?";
cin >> miles;
gallons = liters * LIT_GAL;
cout << "Numbers of liters your car holds is " << liters << " liters.\n";
cout << "Number of miles your car drives without refilling tank " << miles << " miles.\n";
cout << "Press Y for yes or any key for no,\n";
cout << "then hit return.\n";
cin >> again;
}while(again == 'y' || again == 'Y');
return 0;
}
double mpg (double miles, double gallons)
{
return(miles/gallons);
}
I know it's not done but I want to know - Do I have the right idea or doing this all wrong?
-
Looking good so far.
You just need to call your function and print the answer it returns.
And keep an eye on the formatting of the code :)
No one likes to see random indentation of code.
-
Yeah, other than neater code, and calling your function you are pretty much done.
However, quick note to save you some points on the assignment.
You said it says Your program should use a globally defined constant for the numbers of liters per gallon..
How you currently have it, it is not globally defined.
-
Looks good.
You'll want a prototype directive of your function right overtop of main():
Code:
double mpg (double miles, double gallons);