Question

A liter is 0.264179 gallons. Write a program that will read in the number of liters...

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 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. After doing this... Modify your program 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).

Need help modifying this program:

Add two void functions: one called userIntruction that tells the user what to do Another displayResult that displays the original information (number of liters and miles driven) and the result (miles per gallon). Unsure where to go from here, the above program will compile, but I'm a bit unsure how to proceed with the functions required.

Here is my source code:

#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;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#define GALLONS_PER_LITER 0.264179
using namespace std;
class fuel{

  
   double efficiency(int liters,double distance){
       return distance/(liters*GALLONS_PER_LITER);
   }
  
   void userinstruction(){
       cout << "You have to enter the distance travelled by your car in miles and how many litres of gasoline consumed";
       cout << "Enter -1 to quit";
   }
  
   void displayresult(double d1,double d2,int l1,int l2,double avg1,double avg2){
       cout << "Car1 travelled a distance of " << d1 <<endl;
       cout << "Car2 travelled a distance of " << d2 <<endl;
       cout << "Gasoline consumed by car1 " << l1 <<endl;
       cout << "Gasoline consumed by car2 " << l2 <<endl;
       if(avg1 > avg2)
           cout << "Car 1 has greater fuel efficiency";
       else
           cout << "Car 2 has greater fuel efficiency";
   }
int main(void)
{
int liters1 = 0 , liters2 = 0;
double distance1 = 0.0, distance2 = 0.0;

double mpg = 0.0;
do
{
        userinstruction();
cout << "Please input how many liters of gasoline is in your vehicle: ";
cin >> liters1;

cout << "Please input the distance in miles you traveled in your vehicle: ";
cin >> distance1;

cout << "Please input how many liters of gasoline is in your vehicle: ";
cin >> liters2;

cout << "Please input the distance in miles you traveled in your vehicle: ";
cin >> distance2;


double eff1 = efficiency(liters1,distance1);
double eff2 = efficiency(liters2,distance2);

       displayresult(distance1,distance2,liters1,liters2,eff1,eff2);

} while (liters1 > -1);

return 0;
}
};

Add a comment
Know the answer?
Add Answer to:
A liter is 0.264179 gallons. Write a program that will read in the number of liters...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • // Enter your name as a comment for program identification // Program assignment TripCost.cpp // Enter...

    // Enter your name as a comment for program identification // Program assignment TripCost.cpp // Enter your class section, and time /* The program TripCost.cpp uses pointers to calculate    the cost of a trip depending on the cost of a gallon    of gas, the number of miles, and the number of miles    per gallon a car gets. */ /* The cost of gas, the number of miles, and number of    miles per gallon is entered. */...

  • Drivers are concerned with the mileage obtained by their automobiles. One driver has kept track of...

    Drivers are concerned with the mileage obtained by their automobiles. One driver has kept track of several tankfuls of gasoline by recording miles driven and gallons used for each tankful. Develop a C++ program that will input the miles driven and gallons used for each tankful. The program should calculate and display the miles per gallon obtained for each tankful. After processing all input information, the program should calculate and print the combined miles per gallon obtained for all tankfuls....

  • //Vehicle.h #pragma once #include<iostream> #include"Warranty.h" #include<string> using namespace std; class Vehicle{ protected:    string make; int year;   ...

    //Vehicle.h #pragma once #include<iostream> #include"Warranty.h" #include<string> using namespace std; class Vehicle{ protected:    string make; int year;    double mpg;    Warranty warranty;    static int numOfVehicles; public:    Vehicle();    Vehicle(string s, int y, double m, Warranty warranty);    Vehicle(Vehicle& v);    ~Vehicle();    string getMake();    int getYear();    double getGasMileage();    void setMake(string s);    void setYear(int y);    void setYear(string y);    void setGasMileage(double m);    void setGasMileage(string m);    void displayVehicle();    static int getNumVehicles();    Warranty getWarranty();    void setWarranty(Warranty& ); }; //Vehicle.cpp #include "Vehicle.h" #include <string> Vehicle::Vehicle() {    make = "unknown";    year =...

  • In Visual Basics - Program 2. Mileage Write a program which allows the user to enter...

    In Visual Basics - Program 2. Mileage Write a program which allows the user to enter the distance they have traveled by car in miles and the number of gallons of gasoline they used to travel the distance. Be sure to allow for decimal places for both values. Your results should show miles per gallon and kilometers per gallon. A kilometer is 0.61 miles, and this value must be stored as a constant. Your outputs must be formatted to 2...

  • Compute for Miles Per Gallon in C++

    Make a program that will calculate and compute for the quotient of miles and gallons (mpg: miles per gallons). Your program should must ask the user to specify the size of the array (using dynamic array) for the following variable: miles ,gallons and mpg. Prompt the user to Initialize  the value of miles (value for miles should be 100-250) and gallons (values should be from 5-25). Use pointer galPtr for gallons, milPtr for miles and mpgPtr for mpg.  Use function...

  • Write a program to compute miles per gallon of a car, over a long trip. The...

    Write a program to compute miles per gallon of a car, over a long trip. The car begins the trip with a full tank, and each fuel stop along the way fills the tank again. You will not do any math, arithmetic, or computations in the main() function. All output will be displayed from the main() function. Start the program by asking the user for the starting odometer reading. All odometer readings will be in whole miles only. The fuel...

  • I did a program in computer science c++. It worked fine the first and second time...

    I did a program in computer science c++. It worked fine the first and second time when I ran the program, but suddenly it gave me an error. Then, after a few minutes, it started to run again and then the error showed up again. This is due tonight but I do not know what is wrong with it. PLEASE HELP ME! Also, the instruction for the assignment, the code, and the error that occurred in the program are below....

  • Drivers are concerned with the mileage their automobiles get. One driver has kept track of several...

    Drivers are concerned with the mileage their automobiles get. One driver has kept track of several tankfuls of gasoline by recording the miles driven and gallons used for each tankful. Develop a C# app that will input the miles driven and gallons used (both as integers) for each tankful. The app should calculate and display the miles per gallon obtained for each tankful and display the combined miles per gallon obtained for all tankfuls up to this point. All averaging...

  • When the user has finished entering the sales amounts, the program should display the number of...

    When the user has finished entering the sales amounts, the program should display the number of sales amount entered and the average commission. #include <iostream> using namespace std; int main() { const double rate =0.2; int sales = 0; double commission = 0.0; double totalSales =0.0; cout << "First salesperson's sales (-1 to stop) : "; cin >> sales; while (sales !=-1) { commission = sales * rate; cout << "Commission : $ " << commission << endl; cout <<...

  • I need trying to figure out how I can make create a code in Python with this exercise for I am having trouble doing so. Miles Per Gallon Calculator Write a GUI program that calculates a car's gas...

    I need trying to figure out how I can make create a code in Python with this exercise for I am having trouble doing so. Miles Per Gallon Calculator Write a GUI program that calculates a car's gas mileage. The program's window should have Entry widgets that let the user enter the number of gallons of gas the car holds, and the number of miles it can be driven on a full tank. When a Calculate MPG button is clicked,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT