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 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 gallons per liter. Note: A liter is 0.264179 gallons. An example run of the program is shown below: Hints: 1. What will your function do? Document that in the comment. 2. What are the parameter(s) to the function? What type? What type is the returned value? 3. What kind of a loop should you use? What’s the minimum number of times the user will go through the loop? How to write a function 1) Determine the type and number of parameters 2) Determine the type of the return value 3) Declare the function (usually at the top of the program) 4) Write the function (usually at the bottom of the program)
Write a
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 gallons per liter. Note: A liter is 0.264179 gallons. An example run of the program is shown below:
Hints:
1. What will your function do? Document that in the comment.
2. What are the parameter(s) to the function? What type? What type is the returned value?
3. What kind of a loop should you use? What’s the minimum number of times the user will go through the
loop?
How to write a function
1) Determine the type and number of parameters
2) Determine the type of the return value
3) Declare the function (usually at the top of the program)
4) Write the function (usually at the bottom of the program)
Modified Program:
#include <iostream>
using namespace std;
int main(void)
{
const double GALLONS_PER_LITER = 0.264179;
int liters = 0;
double distance = 0.0;
double mpg = 0.0;
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 >> distance;
mpg = distance / (liters * GALLONS_PER_LITER);
cout << "Your vehicle's MPG is: " << mpg << endl;
}while(liters >-1);
return 0;
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images