Running totals and functions

Hello, I'm fairly new at programming and currently working on an assignment. The program takes fuel usage information from the user about a series of trips. After each set of information (for one trip or fueling) is entered, summary information about that trip will be displayed, and then the user will be asked if there is another set. Once the user enters all the sets the program calculates the totals. This is where I run into a problem. When I run the program it will show me the correct running totals for miles and cost. However when it comes to gallons it shows me an incorrect (and incredibly large) number along with displaying NaN for the overall MPG. =S

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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include <iostream>
#include <iomanip>
#define FLEETAVERAGEMPG 25.0

double getGallons (); 
double getMiles ();
double getPricePerGallon ();
double calcTripMileage(double miles, double gallons);
double calcTripCost(double pricePerGallon, double gallons);
double calcTripCostPerMile(double tripCost, double miles);
double calcOverallMPG(double totalMiles, double totalGallons);
void showTotals(double totalMiles, double totalGallons, double totalCost, double overallMPG);
void showOneTrip(double tripMileage, double tripCost, double tripCostPerMile);
void showMileageComparison(double overallMPG);

using namespace std;

//********************************************************************************
int main ()
	{
	double gallons;
	double miles;
	double pricePerGallon;
	double tripCost;
	double tripMileage;
	double tripCostPerMile;
	double totalMiles;
	double totalGallons;
	double totalCost;
	double overallMPG;
	string repeat;
	
	do
	{	 
		gallons = getGallons ();
		miles = getMiles ();
		pricePerGallon = getPricePerGallon ();
		
		tripMileage = calcTripMileage(miles, gallons);
		tripCost = calcTripCost(pricePerGallon, gallons);
		tripCostPerMile = calcTripCostPerMile(tripCost, miles); 	 
		
		cout << endl;
		showOneTrip(tripMileage, tripCost, tripCostPerMile);
		cout << endl;
		
		totalMiles += miles;
		totalGallons += gallons;	
		totalCost += tripCost;
		overallMPG = calcOverallMPG(totalMiles, totalGallons);
			
		cout << "Another Trip? (y/n) "; 
		cin  >> repeat;
		cout << endl;
		
		}
	while (repeat == "Y" ||repeat == "y");
	
	showTotals(totalMiles, totalGallons, totalCost, overallMPG);
	cout << endl;
	showMileageComparison(overallMPG);
	
		
	return 0;
	}
	


//********************************************************************************
double getGallons ()
	{	 
	double gallons; //amount of fuel used 
	
	cout << "Enter the number gallons of fuel: ";
	cin  >> gallons;
	
	while (gallons <0 || gallons  > 40)
		{
		cout << "Out of range: must be between 0 and 40 gallons.\n" 
		     << "Re-enter: ";
		cin  >> gallons;
		}
		
	return gallons;
	}
		
//********************************************************************************
double getMiles ()
	{	 
	double miles; //number of miles traveled during the trip
	
	cout << "Enter the number of miles traveled: ";
	cin  >> miles;
	
	while (miles <0 || miles  > 800)
		{
		cout << "Out of range: must be between 0 and 800 miles.\n" 
		     << "Re-enter: ";
		cin  >> miles;
		}
		
	return miles;
	}
		
//********************************************************************************
double getPricePerGallon ()
	{
	double pricePerGallon; //Price of gas per gallon
	
	cout << "Enter price per gallon for gas: ";
	cin  >> pricePerGallon;
	
	while (pricePerGallon <2.00 || pricePerGallon  > 7.00)
		{
		cout << "Out of range: must be between $2.00 and $7.00.\n" 
		     << "Re-enter: ";
		cin  >> pricePerGallon;
		}
		
	return pricePerGallon;
	}
	
//********************************************************************************
double calcTripMileage (double miles, double gallons)
	{
	double tripMileage; //Miles traveled per gallon of gas
	
	if (gallons == 0)
		{
		tripMileage = 0;
		}
	else if (gallons > 0)
		{
		tripMileage = miles / gallons;
		}
		
	return tripMileage;
	}
	
//********************************************************************************
double calcTripCost(double pricePerGallon, double gallons)
	{
	double tripCost; //Cost of gas on the trip
	
	tripCost = pricePerGallon * gallons;
	
	return tripCost;
	}
	
//********************************************************************************
double calcTripCostPerMile(double tripCost, double miles)
	{
	double tripCostPerMile; //cost of Gas per mile
	
	if (miles == 0)
		{
		tripCostPerMile = 0;
		}
	else if (miles > 0)
		{
		tripCostPerMile = tripCost/miles;
		}
		
	return tripCostPerMile;
	}

//********************************************************************************
double calcOverallMPG(double totalMiles, double totalGallons)
	{
	double overallMPG; //overall MPG for the total trip
	
	if (totalGallons == 0)
		{
		overallMPG = 0;
		}
	else if (totalGallons > 0)	  
		{
		overallMPG = totalMiles/totalGallons;
		}
	}
		
//********************************************************************************
void showOneTrip(double tripMileage, double tripCost, double tripCostPerMile)
	{
	cout << fixed << setprecision (2)
	     << "Trip Milage: " << tripMileage << " mpg\n"
	     << "Trip Cost: $ " << tripCost << endl
		 << "Trip Cost per Mile: $ " << tripCostPerMile << endl; 
	}

//********************************************************************************
void showTotals(double totalMiles, double totalGallons, double totalCost, double overallMPG)
	{
	cout << fixed << setprecision (2)
	     << "Total Miles: " << totalMiles <<endl
	     << "Total Gallons: " << totalGallons <<endl
		 << "Total Cost: $ " << totalCost <<endl
		 << "Overall MPG: " <<overallMPG;
	}
 
//********************************************************************************
void showMileageComparison(double overallMPG)
	{
	double mileageComparison; //compares user's MPG to the fleet's average MPG
	
	mileageComparison = overallMPG - FLEETAVERAGEMPG; 
	
	if (mileageComparison > 0)
		{
		cout << "Your vehicle's mileage is greater than the fleet average by " <<mileageComparison << " mpg.";
		}
	else if (mileageComparison == 0)
		{
		cout << "Your vehicle's mileage is equal to the fleet average of" << FLEETAVERAGEMPG << " mpg.";
		}
	else if (mileageComparison < 0)
		{
		cout << "Your vehicle's mileage is less than the fleet average by " <<mileageComparison << " mpg.";
		}
	}

//******************************************************************************** 


Edit: Found I forgot to put a "+" before the miles in totalMiles = miles
Last edited on
while (gallons <0 || gallons > 40)

If the range is 0-40, don't you want to say
while (gallons >= 0 && gallons <= 40)

Oh, nm. I can't read. :)
Last edited on
I figured out how to get the right running total. I apparently forgot to set the initial values for the total values to 0

Edit: Figured out the 2nd part. Forgot to include the return statement
Last edited on
Topic archived. No new replies allowed.