For the most part everything is executing perfectly. The only thing I cant seem to get to work is the formulas to calculate the miles per gallon; which registers as 0.0 and the miles per dollar; which registers as $0.01.
below are the steps I took and my actual code. Please help! Thanks for any assistance you can provide in advance.
1. Use variables of the correct type.
2. Use accumulators and counters.
3. Using a “while” loop to enter the odometer readings and gallons purchased. The loop should accumulate the number of miles traveled and number of gallons purchased, then calculate the average miles per gallon.
4. Use a “for” loop to ask the user for the price per gallon from three gas stations. Calculate the average price per gallon, and cost to drive one mile.
5. Determine how efficient your car is by comparing the average miles per gallon to the table.
6. Validate input: gallons and gas prices cannot be 0 (or less), a beginning odometer reading must be greater than the prior ending reading (except the first, which must be zero or greater), and an ending reading must be greater than the corresponding beginning reading.
7. Use setprecision and fixed to display prices to 2 decimal places.
8. Enter the following data and screen print the data entry and results:
Execution #1:
Odometer 0 & 100; gallons 5
Odometer 150 & 250; gallons 5
Odometer 300 & 400; gallons 5
Odometer 660 & 760; gallons 5
Prices 2.50, 3.00, 3.50
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
|
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
double startMilege, endMilege, totalMilege, averageMPG, gallons, ppg, totalPPG, countPPG, averagePPG, mpd;
startMilege = 0;
endMilege = 0;
totalMilege = 0;
char advance;
advance = 'y';
while (advance == 'y' || advance == 'Y')
{
cout << "\nEnter the beginning odometer reading> ";
cin >> startMilege;
while (startMilege < endMilege)
{
cout << "\nInput error! starting reading must be greater or equal to " << endMilege << "> ";
cin >> startMilege;
}
cout << "\nEnter the ending odmeter reading> ";
cin >> endMilege;
while (endMilege <= startMilege)
{
cout << "\nInput error! end odometer reading must be greater than starting reading! ";
cin >> endMilege;
}
cout << "\nEnter the gallons required for your fill-up> ";
cin >> gallons;
while (gallons <= 0)
{
cout << "\nInput error! please re-enter reading> ";
cin >> gallons;
}
totalMilege += endMilege - startMilege;
cout << "\nEnter y to enter more readings or n to quit> ";
cin >> advance;
}
averageMPG = totalMilege / gallons; /*This calculation isn't working: it shows up as 0.0*/
totalPPG = 0;
for (countPPG = 0; countPPG < 3; countPPG++)
{
cout << "\nEnter the price per gallon at station #" << countPPG + 1 << " $";
cin >> ppg;
while (ppg <= 0)
{
cout << "\nInput error! please re-enter price $";
cin >> ppg;
}
totalPPG += ppg;
}
averagePPG = totalPPG / 3.0;
mpd = averagePPG / averageMPG; /*Since line 44 isn't calculating correctly neither is this code*/
cout << setprecision(1) << fixed;
cout << "\nYour cars average miles-per-gallon is> " << averageMPG;
cout << setprecision(2) << fixed;
cout << "\nYou bought gas at an average $" << averagePPG << " per gallon";
cout << "\nThe cost for your car to 1 mile is $" << mpd;
if (averageMPG >= 0 && averageMPG <= 15)
{
cout << "\nYour car is very inefficient ";
}
else if (averageMPG >= 16 && averageMPG <= 30)
{
cout << "\nYour car is OK ";
}
else if (averageMPG >= 31)
{
cout << "\nYour car is very efficient ";
}
cout << endl << endl;
system("pause");
return(0);
}
|