Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

write a program that will accept fuel usage information from the user about a se

ID: 3677373 • Letter: W

Question

write a program that will accept fuel usage information from the user about a series of trips. After each set of information (for one trip or fueling) is entered, summary information about that trip will be displayed, and then the user will be asked if there is another set. Multiple sets can be entered. After the user has indicated there are no more sets, certain summary information about all the trips will be displayed.

The main routine has been provided for you as following:

Test your program carefully with various values and ensure that the calculated results are correct. In particular, run the program with different data so you can ensure that the "exceeds" and "is less than" messages appear correctly. Getting the "equal to" result may be difficult due to the approximate nature of floating point arithmetic. We will not test for the "exact" result, but we will look for code to handle that case.

Functions:

Here are the functions you will write and use. Individually, they are all quite short and relatively simple

double getGallons()

This function will include code to prompt for the number of gallons, read the value entered by the user into a local variable, validate it, and return the valid number of gallons.

The function takes no argument. It returns a double value that represents the number of gallons.

Your code must check the value entered by the user to ensure that it is within a certain valid range of values. This means that you will validate the input using a loop (similar to the shoe size example in Part 3 of the lecture notes). If the value entered for gallons is not between 0.0 and 40.0 (inclusive), display an error message such as:

and then read a new value from the user. Do this repeatedly until a valid value is entered. We will not check for non-numeric input in this program.

double getMiles()

Similar to the function described above, but for miles. A valid value for miles is between 0.0 and 800.0 (inclusive).

double getPricePerGallon()

Ditto, but for gas price. A valid value is between 2.00 and 7.00 (inclusive).

double calcTripMileage(double miles, double gallons)

This short function will calculate and return a mileage given the number of miles and the number of gallons as arguments. However, if the number of gallons is less than or equal to 0, it will return 0. The calling program will then report 0 as the mileage.

The function takes two double arguments: the number of miles traveled and the number of gallons of fuel that were used. It returns a double value that represents the mileage (miles per gallon).

double calcTripCost(double pricePerGallon, double gallons)

This one-line function calculates a trip cost from the two arguments and returns it.

The function takes two double arguments: the price per gallon for the fuel and the number of gallons of fuel that were used. It returns a double value that represents the cost of the fuel for the trip.

double calcTripCostPerMile(double tripCost, double miles)

Again, does what it says, for one trip. However, if miles is 0, return 0.

The function takes two double arguments: the cost of the fuel for the trip and the number of miles traveled. It returns a double value that represents the cost for each mile or the trip.

double calcOverallMPG(double totalMiles, double totalGallons)

This function calculates the miles per gallon for the trip. However, if totalGallons is 0, return 0.

The function takes two double arguments: the total number of miles traveled and the total number of gallons of fuel used. It returns a double value that represents the number of miles traveled per gallon of fuel.

void showOneTrip(double tripMileage, double tripCost, double tripCostPerMile)

This function simply displays the information for a single trip. The information to be displayed are the three passed in arguments. They should be displayed in a nicely formatted table with labels.

The function takes three double arguments: the trip mileage, the cost of the trip, and the cost per mile for the trip. It returns nothing.

void showTotals(double totalMiles, double totalGallons, double totalCost, double overallMPG)

This function displays the totals for all trips entered. The information to be displayed are the four passed in arguments. They should be displayed in a nicely formatted table with labels.

The function takes four double arguments: the total number of miles traveled, the total number of gallons of fuel used, the total cost of the trip, and the number of miles per gallon for all of the trips. It returns nothing.

void showMileageComparison(double overallMPG)

This function compares the miles per gallon for all the trips with the (symbolic constant) fleet average and displays a message indicating whether the miles per gallon is less than, greater than, or equal to the fleet average. It should also display the difference between the fleet average and the mileage for the set of trips.

The function takes one double argument: the number of miles per gallon for all of the trips. It returns nothing.

Program Requirements

Each function must have a documentation box explaining:

its name

its use or function: that is, what does it do? What service does it provide to the code that calls it?

a list of its arguments briefly describing the meaning and use of each, and in particular whether the function modifies each one

the value returned (if any) or none

notes on any unusual features, assumptions, techniques, etc.

Complete program documentation is required. For this assignment, that means that line documentation AND function documentation boxes are needed. In regards to line documentation, there is no need to document every single line, but logical "chunks" of code should be preceded by a line or two that describe what the "chunk" of code does.

The main routine that has been provided may not be altered in the source code that is graded. However, as the program is being developed, you may comment out some of the function calls so that the functions can be coded one at a time. You may also want to think about making the loop execute fewer times as you're developing the functions.

Output should be like the following:

Explanation / Answer


#include <iostream>
#include <iomanip>

using namespace std;

double const FLEET_AVG = 25.0;

// Your function Prototypes go here
/***************************************************************
Function: double getGallons()

Use: This function gets the gallons of fule used in a trip from the user between the numbers 0 and 40

Returns: the users inputed gallons
***************************************************************/
double getGallons();

/***************************************************************
Function: double getMiles()

Use: This function gets the miles traveled in a trip from the user between the numbers 0 and 800

Returns: the users inputed miles
***************************************************************/
double getMiles();

/***************************************************************
Function: double getPricePerGallon()

Use: This function gets the the price for a gallon of fule from the user between the numbers 2 and 7

Returns: the users inputed cost
***************************************************************/
double getPricePerGallon();

/***************************************************************
Function: double calcTripMileage(double, double)

Use: This function calculates the mileage for a single trip

Arguments: double: the number of miles traveled
           double: the number of gallons of fuel used for the trip

Returns: a double value that represents the number of miles per gallon
***************************************************************/
double calcTripMileage(double, double);

/***************************************************************
Function: double calcTripCost(double, double)

Use: This function calculates the cost of a trip

Arguments: double: the price per gallon on the trip
           double: the number of gallons of fuel used for the trip

Returns: a double value that represents the cost of the trip
***************************************************************/
double calcTripCost(double, double);

/***************************************************************
Function: double calcTripCost(double, double)

Use: This function calculates the cost per mile of a trip

Arguments: double: total cost of the trip
           double: the number of miles traveled for the trip

Returns: a double value that represents the cost per mile per trip
***************************************************************/
double calcTripCostPerMile(double, double);

/***************************************************************
Function: double calcOverallMPG(double, double)

Use: This function calculates the overall MPG

Arguments: double: total miles traveled on the trip
           double: the number of gallons of fuel used for the trip

Returns: a double value that represents the overall MPG
***************************************************************/
double calcOverallMPG(double, double);

/***************************************************************
Function: void showOneTrip(double, double, double)

Use: This function diplays information for one trip

Arguments: double: total trip miles
           double: total trip cost
           double: total trip cost per mile

***************************************************************/
void showOneTrip(double, double, double);

/***************************************************************
Function: void showTotals(double, double, double, double)

Use: This function displays totals for data across multible trips

Arguments: double: total miles across all trips
           double: total gallons of gas across all trips
           double: total cost of all trips
           double: overall MPG across all trips
         
***************************************************************/
void showTotals(double, double, double, double);

/***************************************************************
Function: void showMileageComparison(double)

Use: This function compairs the users MPG agents the avarage MPG

Arguments: double: overall MPG across all trips

***************************************************************/
void showMileageComparison(double);

int main() {
   double    gallons         = 0,       //Place holder for user inputs
              miles         = 0,      
              pricePerGallon    = 0;      
  
   double    tripMileage    = 0,       //Place holder for one trip stats
              tripCost       = 0,      
              tripCostPerMile = 0;      
  
   double    totalGallons   = 0,       //Place holder for stats for all trips
           totalMiles      = 0,      
           totalCost      = 0;      
  
   double   overallMPG      = 0;       //Place holder for Overall Miles Per Gallon
  
   char   another;                    //Holds user's answer
  
   //Print Title
   cout << "     Fuel Usage Analysis" << endl << endl;
  
   //Start do... while loop to process unknown number of trips
   do {
       //Get trip values
       gallons = getGallons();
       miles = getMiles();
       pricePerGallon = getPricePerGallon();
      
       cout << endl;
      
       //Calculate trip costs
       tripMileage = calcTripMileage(miles , gallons);
       tripCost = calcTripCost(pricePerGallon , gallons);
       tripCostPerMile = calcTripCostPerMile(tripCost , miles);
      
       //Accumulate totals
       totalGallons += gallons;
       totalMiles += miles;
       totalCost += tripCost;
      
       overallMPG = calcOverallMPG(totalMiles, totalGallons);

        //Display stats for one trip
       showOneTrip(tripMileage, tripCost, tripCostPerMile);
      
  
       cout << "Another? (y or n) ";
       cin >> another;
      
   }
   while (another == 'y' || another =='Y');
  
  
   //Print totals of entered trips
   showTotals(totalMiles, totalGallons, totalCost, overallMPG);
  
   //Compare overall mpg from entered trips and compare with fleet average
   showMileageComparison(overallMPG);
  
  
   return 0;
}
//Your function definitions go here


double getGallons(){
   cout << "Enter the number of gallons of fuel: ";
   double input;
   cin >> input;
   while (!(input >= 0.0 && input <= 40.0)){
      
       cout << "Out of range: must be between 0 and 40 gallons. Re-enter: ";
       cin >> input;
   }
   return input;
}


double getMiles(){
   cout << "Enter the number of miles: ";
   double input;
   cin >> input;
   while (!(input >= 0.0 && input <= 800.0)){
       cout << "Out of range: must be between 0 and 800 miles. Re-enter: ";
       cin >> input;
   }
   return input;
}


double getPricePerGallon(){
   cout << "Enter the price per gallon: ";
   double input;
   cin >> input;
   while (!(input >= 2.0 && input <= 7.0)){
       cout << "Out of range: must be between 2.00 and 7.00 dollars. Re-enter: ";
       cin >> input;
   }
   return input;
}


double calcTripMileage(double miles, double gallons){
   if(gallons == 0)
       return 0;
   double temp = miles/gallons;
   if (temp < 0)
       return 0;
   else
       return temp;
}


double calcTripCost(double pricePerGallon, double gallons){
   return pricePerGallon * gallons;
}


double calcTripCostPerMile(double tripCost, double miles){
   if(miles == 0)
   return 0;
   else
   return tripCost/miles;
}


double calcOverallMPG(double totalMiles, double totalGallons){
   if(totalGallons == 0)
   return 0;
   else
   return totalMiles/totalGallons;
}


void showOneTrip(double tripMileage, double tripCost, double tripCostPerMile){
   cout << "Trip Mileage:            " <<fixed<< setprecision(2) << setw(5) << tripMileage << " mpg";
   cout << " Trip Cost:              $" << setw(5) << tripCost;
   cout << " Trip Cost per mile:     $" <<fixed<< setprecision(3) << setw(6) << tripCostPerMile << endl<< endl;  
}


void showTotals(double totalMiles, double totalGallons, double totalCost, double overallMPG){
   cout << "Total Miles:" <<fixed<< setprecision(2) << setw(11) << totalMiles;
   cout << " Total Gallons:" << setw(9) <<totalGallons;
   cout << " Total Cost:      $" << setw(5) <<totalCost;
   cout << " Overall MPG:"   << setw(11) <<overallMPG;
}


void showMileageComparison(double overallMPG){
   cout << " Your vehicle's mileage is greater than fleet average by " << overallMPG - FLEET_AVG << " mgp";
}


output


   Fuel Usage Analysis                                                                                                                                                         
Enter the number of gallons of fuel: 25                                                                                                                     
Enter the number of miles: 4                                                                                                                                
Enter the price per gallon: 7                                                                                                                               
                                                                                                                                                            
Trip Mileage:             0.16 mpg                                                                                                                          
Trip Cost:              $175.00                                                                                                                             
Trip Cost per mile:     $43.750                                                                                                                             
                                                                                                                                                            
Another? (y or n) n                                                                                                                                         
Total Miles:       4.00                                                                                                                                     
Total Gallons:    25.00                                                                                                                                     
Total Cost:      $175.00                                                                                                                                    
Overall MPG:       0.16                                                                                                                                     
                                                                                                                                                            
Your vehicle's mileage is greater than fleet average by -24.84 mgpsh-4.3$