1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
double startingMiles, endMiles, totalMiles, averageMiles, ppg, totalPPG, countPPG, averagePPG, perMile, gallons, totalGallons;
startingMiles = 0;
endMiles = 0;
totalMiles = 0;
totalGallons = 0.0;
/**********************/
int numGasStations = 0; // Declare and Initialize the variable accumulator.
/**********************/
char more;
more = 'y';
while (more == 'y' || more == 'Y') // allow both Y and y answer
{
cout << "\n Enter the beginning odometer reading >"; // Starting Reading
cin >> startingMiles;
while (startingMiles < endMiles)
{
cout << "\n Input error! end odometer reading must be greater than the last reading! " << endl; // If Start is Greater than End, Output Error
cout << "Enter the ending odometer reading > ";
cin >> endMiles;
}
cout << "\n Enter the ending odmeter reading >"; //End Reading
cin >> endMiles;
while (endMiles <= startingMiles)
{
cout << "\n Input error! end odometer reading must be greater than starting reading! " << endl; // If Start is Greater than End, Output Error
cout << "Enter the ending odometer reading > ";
cin >> endMiles;
}
cout << "\n Enter the gallons required for your fill-up > "; // Gallons in Fill up
cin >> gallons;
totalGallons += gallons; //accumulate running gallons total
while (gallons <= 0)
{
cout << "\n Input error! Please Re-Enter > ";
cin >> gallons;
}
totalMiles += endMiles - startingMiles; // calculate total miles driven
cout << endl;
cout << "Enter y to enter more readings or n to quit> "; // Does customer have more inputs
cin >> more;
/***********************************/
if (more == 'Y' || more == 'y') // If yes, then the accumulator will add 1 gas stop
numGasStations++;
else if (more == 'N' || more == 'n') // If no, then there will be AT LEAST 1 gas stop.
numGasStations = 1;
/***********************************/
if (more != 'Y' && more != 'y' && more != 'N' && more != 'n') // Validate input allowing only YyNn answers
{
cout << "Invalid entry. Please enter Y or N";
cin >> more;
}
}
cout << endl;
averageMiles = totalMiles / totalGallons; // Calculate the averageMiles
totalPPG = 0;
/*************************************/
// Change it from 3 to numGasStations to loop as many as required, pending the answer from the user
// To add more data.
for (countPPG = 0; countPPG < numGasStations; countPPG++) //Run the loop up to the number of gas stations.
{
cout << "\n Enter the price per gallon at station #" << countPPG + 1 << " $";
cin >> ppg;
while (ppg <= 0)
{
cout << "\n That is not a valid price. Please Enter Again $" << endl;
cin >> ppg;
}
totalPPG += ppg;
}
averagePPG = totalPPG / countPPG; // calculate average miles per gallon
perMile = averagePPG / averageMiles;
cout << endl;
cout << setprecision(1) << fixed;
cout << "Your cars average miles-per-gallon is> " << averageMiles << endl;
cout << setprecision(2) << fixed;
cout << "You bought gas at an average $" << averagePPG << " per gallon" << endl;
cout << "The cost for your car to 1 mile is $" << perMile << endl;
if (averageMiles >= 0 && averageMiles <= 15) // determine effeciency of vehicle
{
cout << "Your car is Very Inefficient " << endl;
}
else if (averageMiles >= 16 && averageMiles <= 30)
{
cout << "Your car is Reasonably Effecient " << endl;
}
else if (averageMiles >= 31)
{
cout << "Your car is Very Efficient " << endl;
}
system("pause");
return(0);
}
|