Hey everyone I'm new to C++ programming and I have a dilemma. How do I get this program to add the totals of all the data entries to get the grand total? I am stumped! Any help would be greatly appreciated!
Thanks,
JR
// -- basic opening commands --
#include <iostream>
#include <iomanip>
using namespace std;
const double FLEET_AVG_MPG = 25.0;
// -- variable declarations --
int main()
{
double gallonsUsed,
milesTraveled,
pricePerGallon,
tripMileage,
tripCost,
tripCostPerMile,
totalMiles,
totalGallons,
totalCost,
totalMPG,
lessThan,
greaterThan;
char choice;
cout << "\n\n Fuel Usage Analysis\n";
// -- output statements asking for user input information --
do
{
cout << "\nEnter the number of gallons of fuel: ";
cin >> gallonsUsed;
while (gallonsUsed < 0.0 or gallonsUsed > 40.00)
{
cout << "\nOut of range: must be between 0 and 40 gallons. Re-enter: ";
cin >> gallonsUsed;
}
cout << "\nEnter the number of miles: ";
cin >> milesTraveled;
while (milesTraveled < 0.0 or milesTraveled > 800.0)
{
cout << "\nOut of range: must be between 0 and 800 miles. Re-enter: ";
cin >> milesTraveled;
}
cout << "\nEnter the price per gallon: ";
cin >> pricePerGallon;
while (pricePerGallon < 1.00 or pricePerGallon > 5.00)
{
cout << "\nOut of rang: must be between 1 and 5 dollars. Re-enter: ";
cin >> pricePerGallon;
}
// -- formulas --
tripMileage = (milesTraveled / gallonsUsed);
tripCost = (gallonsUsed * pricePerGallon);
tripCostPerMile = (tripCost / milesTraveled);
totalMiles = (milesTraveled);
totalGallons = (gallonsUsed);
totalCost = (tripCost);
totalMPG = (totalMiles / totalGallons);
lessThan = (FLEET_AVG_MPG - totalMPG);
greaterThan = (totalMPG - FLEET_AVG_MPG);
// -- data set analysis --
cout << fixed << setprecision(2)
<< "\nTrip Mileage: " << tripMileage << " mpg";
cout << fixed << setprecision(2)
<< "\nTrip Cost: $ " << tripCost;
cout << fixed << setprecision(3)
<< "\nTrip Cost per mile: $ " << tripCostPerMile;
cout << "\nAnother? (y or n) ";
cin >> choice;
cout << "\n";
}
while (choice == 'y');
// -- totals of all data sets --
cout << fixed << setprecision(2)
<< "\nTotal Miles: " << totalMiles;
cout << fixed << setprecision(2)
<< "\nTotal Gallons: " << totalGallons;
cout << fixed << setprecision(2)
<< "\nTotal Cost: $ " << totalCost;
cout << fixed << setprecision(2)
<< "\nOverall MPG: " << totalMPG << "\n";
if (totalMPG < 25.0)
cout << "\nYour vehicle's mileage is less than the fleet average by " << lessThan << " mpg";
else if (totalMPG > 25.0)
cout << "\nYour vehicle's mileage is greater than the fleet average by " <<greaterThan << " mpg";
else
cout << "\nYour vehicle's mileage is equal to the fleet average of " << FLEET_AVG_MPG << " mpg";
cout << "\n";
return 0;
}