Fuel costs Loop Assignment assistance

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);
}
Last edited on
It runs fine for me...

here is the input/output


Enter the beginning odometer reading> 0

Enter the ending odmeter reading> 100

Enter the gallons required for your fill-up> 5

Enter y to enter more readings or n to quit> y

Enter the beginning odometer reading> 150

Enter the ending odmeter reading> 250

Enter the gallons required for your fill-up> 5

Enter y to enter more readings or n to quit> y

Enter the beginning odometer reading> 300

Enter the ending odmeter reading> 400

Enter the gallons required for your fill-up> 5

Enter y to enter more readings or n to quit> y

Enter the beginning odometer reading> 660

Enter the ending odmeter reading> 760

Enter the gallons required for your fill-up> 5

Enter y to enter more readings or n to quit> n

Enter the price per gallon at station #1 $2.50

Enter the price per gallon at station #2 $3.00

Enter the price per gallon at station #3 $3.50

Your cars average miles-per-gallon is> 80.0
You bought gas at an average $3.00 per gallon
The cost for your car to 1 mile is $0.04
Your car is very efficient



I personally like to simplify my statements when compare either lowercase or uppercase. This will check "advance" as always being lowercase even if uppercase is used.
http://www.cplusplus.com/reference/cctype/tolower/
Line 14
while (tolower(advance) == 'y')
Line 18: How can you compare startMileage and endMileage when you haven't entered endMileage yet?
Did you figure this out? I am working on the same thing, and I am having the same trouble. I think it is because "gallons" is not adding correctly. When I debug, the galllons always saves as the last input of gallons, not all gallons added together.
You forgot to initialize a "totalFuel" variable. Currently all you are doing is overwriting the same fuel variable repeatedly, and dividing your totalMileage by the singular iteration of gallons in line 44. You would need to create a variable for totalFuel, add you running total of gallons to it with each pass through the loop (totalFuel += gallons) and then change line 44 to be dividing the totalMileage by your totalFuel. You'll should get a "miles-per-gallon" of 20 if you use the same input as above.
Last edited on
Topic archived. No new replies allowed.