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

CAN SOMEONE PLEASE FIX THIS ALMOST RIGHT, GLITCHES AFTER SECOND COMMAND #include

ID: 3929413 • Letter: C

Question

CAN SOMEONE PLEASE FIX THIS ALMOST RIGHT, GLITCHES AFTER SECOND COMMAND

#include <iostream>

using namespace std;

int main()

{

            int milesDriven;

            int gallonsUsed;

            int milesTotal = 0;

            int gallonsTotal = 0;

            cout << "Enter miles driven (-1 to quit): ";

            cin >> milesDriven;

            while (milesDriven != -1)

            {

                        cout << "Enter gallons used: ";

                        cin >> gallonsUsed;

                        milesTotal += milesDriven;

                        gallonsTotal += gallonsUsed;

                        cout << "MPG this trip: " << static_cast<double>(milesDriven) / gallonsUsed << endl;

                        cout << "Total MPG: " << static_cast<double>(milesTotal) / gallonsTotal << endl << endl;

                        cout << "Enter miles driven (-1 to quit): ";

                        cin >> milesDriven;

            }         

}

OBJECTIVE

Drivers are concerned with the mileage obtained by their automobiles. One driver has kept track of several tankful's 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 tankful's.

Output Enter the gallons used (-1 to end): 16

Enter the miles driven: 220

The Miles / Gallon for this tank was 13.75

Enter the gallons used (-1 to end): 16.5

Enter the miles driven: 272

The Miles / Gallon for this tank was 16.4848

Enter the gallons used (-1 to end): -1

The overall average Miles/Gallon was 15.1385

Explanation / Answer

#include <iostream>
using namespace std;
#include <stdlib.h>
int main()
{
int milesDriven;
double gallonsUsed; //double gallonsUsed
int milesTotal = 0;
double gallonsTotal = 0; //double gallonsTotal
  
cout << "Enter miles driven (-1 to quit): ";
cin >> milesDriven;
while(milesDriven != -1)
{
cout << " Enter gallons used: ";
cin >> gallonsUsed;
milesTotal += milesDriven;
gallonsTotal += gallonsUsed;
cout << " MPG this trip: " << milesDriven/gallonsUsed << endl;
  
cout << "Enter miles driven (-1 to quit): ";
cin >> milesDriven;
if(milesDriven == -1) //exit the programif milesDriven =-1
{
break;
exit(0);
}

}
cout << " Total MPG: " << milesTotal/gallonsTotal << endl << endl;
}

output:

Success time: 0 memory: 3472 signal: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