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

You will be creating a program that calculates a fleet\'s car and truck miles pe

ID: 3750057 • Letter: Y

Question

You will be creating a program that calculates a fleet's car and truck miles per gallon.

Create a program that continually asks for a command, which will be "car", "truck", or "done". If the user does not specify one of these commands, output "Unknown command."

If the user types "done", calculate the average miles per gallon of all of the given cars and calculate the average miles per gallon of all of the given trucks. If the user did not provide any cars, output "Fleet has no cars." If the user did not provide any trucks, output "Fleet has no trucks." Otherwise, output "Average car MPG = " and "Average truck MPG = ".If the user types "truck" or "car", then ask the user for the number of miles and number of gallons of fuel used on either the truck or car. This will be a running total. You will need to store the total number of miles, total number of gallons, and total number of cars and trucks.You must also make a check to ensure the user gives you proper input. If improper input was given, keep asking the user to give you legitimate values until they comply (see examples below).

1. car, truck, and done are the only commands accepted. Car is not a valid command, and neither is Truck.

2. You do not need to strip off the end of a valid number, such as 18.2dz. The extraction operator will extract 18.2 and leave dz in the stream, which will need to be ignored later since it will try to match the next command.

Explanation / Answer

//C++ program

#include<iostream>

using namespace std;

int main(){

string command;

int car=0,truck=0;

double milec=0,milet=0 ,gallonsc=0,gallonst=0,val;

  

while(1){

while(1){

cout<<" Enter command : ";

cin>>command;

if(command=="car"||command=="truck"||command=="done")break;

if(command=="")cout<<" UNKNOWN COMMAND ";

else cout<<" Invalid command";

}

if(command=="car"){

car++;

cout<<" Enter car's miles: ";

cin>>val;

milec+=val;

cout<<" Enter car's gallons: ";

cin>>val;

gallonsc+=val;

}

if(command=="truck"){

truck++;

car++;

cout<<" Enter truck's miles: ";

cin>>val;

milet+=val;

cout<<" Enter truck's gallons: ";

cin>>val;

gallonst+=val;

}

else if(command=="done")break;

}

if(command=="done"){

if(car>0)

cout<<"Average car MPG = "<<(milec/gallonsc)<<" ";

else cout<<"Fleet has no cars. ";

if(truck>0)

cout<<"Average car MPG = "<<(milet/gallonst)<<" ";

else cout<<"Fleet has no trucks. ";

}

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote