Outut Error

I am trying to get my program to calculate a set of numbers and when done, I want it to show a screen to summarize the runs. The only problem I am getting now is that I am getting all 0's in my output and NaN in the summary section. Can anyone please help!?!?!?


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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include <iostream>
#include <iomanip>
#include <cmath>

const double FleetAvg = 25.0;


double tripMileage;
double CostPerMile;
double TripCost;
double overallMPG;
double totalMiles = 0;
double totalGallons = 0;
double totalCost = 0;
double gallons;
double miles;
double price;
double MileageComp;

char ANSWER;


double getGallons();
double getMiles();
double getPricePerGallon();
double calcTripMileage (double miles, double gallons);
double calcTripCost (double price, double gallons);
double calcTripCostPerMile (double TripCost, double miles);
double calcOverallMPG (double totalMiles, double totalGallons);
void showOneTrip (double TripMileage, double TripCost, double TripCostPerMile);
void showTotals (double totalMiles, double totalGallons, double totalCost, double overallMPG);
void showMileageComparison (double overallMPG);


using namespace std;


int main()

{
	cout << "     Fuel Usage Analysis";
	cout << "\n\n";
	
do 
{
		gallons = getGallons ();
		miles = getMiles ();	
		price = getPricePerGallon ();
			
		cout << "\n\n";
		showOneTrip (tripMileage, TripCost, CostPerMile);
		cout << "\n\n";
		
	calcTripMileage (miles, gallons);	 
	calcTripCost (price, gallons);
	calcTripCostPerMile (TripCost, miles);	   
	
		
	 totalMiles += miles;
	 totalGallons += gallons;
	 totalCost += TripCost;
	
		overallMPG = calcOverallMPG (totalMiles, totalGallons);
				
		cout << "Another? (y or n) ";
		cin >> ANSWER;
			
}

while((ANSWER == 'Y')||(ANSWER == 'y'));

cout << "\n\n";
	
	showTotals (totalMiles, totalGallons, totalCost, overallMPG);
	
cout << "\n\n";

	showMileageComparison (overallMPG);	   
	

return 0;

}

/***********
  FUNCTIONS
***********/

double getGallons()
{

double gallons;

	cout << "Enter the number of gallons of fuel: ";
	cin >> gallons;
		
	while (gallons <= 0 || gallons > 40)
		{
			cout << "\n\n";
			cout << "Out of range: must be between 0 and 40 gallons. Re-enter: ";
			cin >> gallons;
			cout << "\n";
		}
}

double getMiles()
{
	
double miles;

	cout << "Enter the number of miles: ";
	cin >> miles;	
		
	while (miles <= 0 || miles > 800)
		{
			cout << "\n\n";
			cout << "Out of range: must be between 0 and 800 miles. Re-enter: ";
			cin >> miles; 
			cout << "\n";	 	 	 
		}
}

double getPricePerGallon()
{

double price;

	cout << "Enter the price per gallon: ";
	cin >> price;
	
	while (price <= 2 || price > 7)
		{
			cout << "\n\n";
			cout << "Out of range: must be between $2.00 and $7.00 dollars. Re-enter: ";
			cin >> price;
			cout << "\n";
		}
	return price;
}

double calcTripMileage (double miles, double gallons)
{

double tripMileage;

	if (gallons == 0)
		{
			tripMileage = 0;
		}
	else if (gallons > 0)
		{
			tripMileage = miles / gallons;
		}
	return tripMileage;
}

double calcTripCost (double price, double gallons)
{

 double TripCost;
 
 	TripCost = price * gallons;
	
	return TripCost;
}

double calcTripCostPerMile (double TripCost, double miles)
{

double CostPerMile;

	if (miles == 0)
		{
			CostPerMile = 0;
		}
	else if (miles > 0)
		{
			CostPerMile = TripCost / miles;
		}
	return CostPerMile;
	
}


double calcOverallMPG (double totalMiles, double totalGallons)
{

double overallMPG;

	if (totalGallons == 0)
		{
			overallMPG = 0;
		}
	else if (totalGallons > 0)
		{
			overallMPG = totalMiles/totalGallons;
		}
	return overallMPG;
				
}



void showOneTrip (double TripMileage, double TripCost, double TripCostPerMile)
{


	cout << "Trip Mileage:           " << fixed << showpoint << setprecision(2) << tripMileage << " MPG" << endl;
	cout << "Trip Cost:             $" << fixed << showpoint << setprecision(2) << TripCost << endl;
	cout << "Trip cost per mile:    $" << fixed << showpoint << setprecision(2) << CostPerMile << endl;
	
}


void showTotals (double totalMiles, double totalGallons, double totalCost, double overallMPG)
{

	cout << "Total Miles:     " << totalMiles << endl;
	cout << "Total Gallons:     " << totalGallons << endl;
	cout << "Total Cost:     " << totalCost << endl;
	cout << "Overall MPG:     " << overallMPG << endl;
}

void showMileageComparison (double overallMPG)
{

	double MileageComp = overallMPG - FleetAvg;

		if (MileageComp > 0)
			{
				cout <<	 "Your vehicle's mileage is greater than the fleet average by " << MileageComp << "mpg.";
			}
		else if (MileageComp == 0)
			{
				cout << "Your vehicle's mileage is equal to the fleet average of " << FleetAvg << "mpg.";
			}
		else if (MileageComp < 0)
			{
				cout << "Your vehicle's mileage is is less than the fleet average by " << MileageComp << "mpg.";
			}
}

I can tell you without reading it that your issue is your global variables.

Edit: That's not causing your issues, but that is a horrible practice. After running it and checking the warnings, I feel you might understand your issue better after looking at them yourself:
C:\Programming\main.cpp||In function 'double getGallons()':|
C:\Programming\main.cpp|104|warning: no return statement in function returning non-void [-Wreturn-type]|
C:\Programming\main.cpp||In function 'double getMiles()':|
C:\Programming\main.cpp|121|warning: no return statement in function returning non-void [-Wreturn-type]|
C:\Programming\main.cpp|204|warning: unused parameter 'TripMileage' [-Wunused-parameter]|
C:\Programming\main.cpp|204|warning: unused parameter 'TripCostPerMile' [-Wunused-parameter]|


To put it a little simpler, you're telling the compiler that you're going to be returning data types from these functions, but no data is actually getting returned. Look at each function that has something other than void in front of it.
Last edited on

Volatile Pulse (416)

Jul 9, 2012 at 10:46pm



I can tell you without reading it that your issue is your global variables.


I don't understand, which variables are wrong? I assumed you have to declare every function before writing them so I added them. Every other variable I thought was inthe right place. If anyone could help with detailing what I am doing wrong that would be great!

EDIT:
WOW I figured out why the summary wasn't working. I now see what I was doing wrong with the variables. Now the first set still shows 0's and I am thinking I am not writting the

void showOneTrip (double tripMileage, double TripCost, double CostPerMile)

function correctly. I am currently trying to figure out what is wrong with this but I wanted to say THANK YOU SO MUCH!!!!
Last edited on
Declaring your variables where they're at makes them global variables, you do not need to pass them to functions, as you do in your function prototypes. I suggest picking something you prefer, either passing variables to functions, which is the proper way to do it, or to use global variables, which is mainly what you're doing now.
What Volatile Pulse means is that some of your functions that "return something" do not actually "return something". That is, there is no return statement in the body of these functions. Actually, these functions are listed in the errors, for e.g., getGallons() doesn't return anything, I believe you want it to return gallons. Could you fix those?



Volatile Pulse (417)

Jul 9, 2012 at 11:00pm



Declaring your variables where they're at makes them global variables, you do not need to pass them to functions, as you do in your function prototypes. I suggest picking something you prefer, either passing variables to functions, which is the proper way to do it, or to use global variables, which is mainly what you're doing now.




Report





ToniAz (143)

Jul 9, 2012 at 11:04pm



What Volatile Pulse means is that some of your functions that "return something" do not actually "return something". That is, there is no return statement in the body of these functions. Actually, these functions are listed in the errors, for e.g., getGallons() doesn't return anything, I believe you want it to return gallons. Could you fix those?




Report


Yes, That is what I fixed. I stil haven't changed the function decleration but I fixed the return parts (I think) I will post the updated file so you can see the new errors.

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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246

#include <iostream>
#include <iomanip>
#include <cmath>

const double FleetAvg = 25.0;


double tripMileage;
double CostPerMile;
double TripCost;
double overallMPG;
double totalMiles = 0;
double totalGallons = 0;
double totalCost = 0;
double gallons;
double miles;
double price;
double MileageComp;

char ANSWER;


double getGallons();
double getMiles();
double getPricePerGallon();
double calcTripMileage (double miles, double gallons);
double calcTripCost (double price, double gallons);
double calcTripCostPerMile (double TripCost, double miles);
double calcOverallMPG (double totalMiles, double totalGallons);
void showOneTrip (double TripMileage, double TripCost, double TripCostPerMile);
void showTotals (double totalMiles, double totalGallons, double totalCost, double overallMPG);
void showMileageComparison (double overallMPG);


using namespace std;


int main()

{
	cout << "     Fuel Usage Analysis";
	cout << "\n\n";
	
do 
{
		gallons = getGallons ();
		miles = getMiles ();	
		price = getPricePerGallon ();
	
	calcTripMileage (miles, gallons);	 
	calcTripCost (price, gallons);
	calcTripCostPerMile (TripCost, miles);	  
		 	 
		cout << "\n\n";
		showOneTrip (tripMileage, TripCost, CostPerMile);
		cout << "\n\n";
		
	 totalMiles += miles;
	 totalGallons += gallons;
	 totalCost += TripCost;
	
		overallMPG = calcOverallMPG (totalMiles, totalGallons);
				
		cout << "Another? (y or n) ";
		cin >> ANSWER;
			
}

while((ANSWER == 'Y')||(ANSWER == 'y'));

cout << "\n\n";
	
	showTotals (totalMiles, totalGallons, totalCost, overallMPG);
	
cout << "\n\n";

	showMileageComparison (overallMPG);	   
	

return 0;

}

/***********
  FUNCTIONS
***********/

double getGallons()
{

double gallons;

	cout << "Enter the number of gallons of fuel: ";
	cin >> gallons;
		
	while (gallons <= 0 || gallons > 40)
		{
			cout << "\n\n";
			cout << "Out of range: must be between 0 and 40 gallons. Re-enter: ";
			cin >> gallons;
			cout << "\n";
		}
	return gallons;
}

double getMiles()
{
	
double miles;

	cout << "Enter the number of miles: ";
	cin >> miles;	
		
	while (miles <= 0 || miles > 800)
		{
			cout << "\n\n";
			cout << "Out of range: must be between 0 and 800 miles. Re-enter: ";
			cin >> miles; 
			cout << "\n";	 	 	 
		}
	return miles;
}

double getPricePerGallon()
{

double price;

	cout << "Enter the price per gallon: ";
	cin >> price;
	
	while (price <= 2 || price > 7)
		{
			cout << "\n\n";
			cout << "Out of range: must be between $2.00 and $7.00 dollars. Re-enter: ";
			cin >> price;
			cout << "\n";
		}
	return price;
}

double calcTripMileage (double miles, double gallons)
{

double tripMileage;

	if (gallons == 0)
		{
			tripMileage = 0;
		}
	else if (gallons > 0)
		{
			tripMileage = miles / gallons;
		}
	return tripMileage;
}

double calcTripCost (double price, double gallons)
{

 double TripCost;
 
 	TripCost = price * gallons;
	
	return TripCost;
}

double calcTripCostPerMile (double TripCost, double miles)
{

double CostPerMile;

	if (miles == 0)
		{
			CostPerMile = 0;
		}
	else if (miles > 0)
		{
			CostPerMile = TripCost / miles;
		}
	return CostPerMile;
	
}


double calcOverallMPG (double totalMiles, double totalGallons)
{

double overallMPG;

	if (totalGallons == 0)
		{
			overallMPG = 0;
		}
	else if (totalGallons > 0)
		{
			overallMPG = totalMiles/totalGallons;
		}
	return overallMPG;
				
}



void showOneTrip (double tripMileage, double TripCost, double CostPerMile)
{


	cout << "Trip Mileage:           " << fixed << showpoint << setprecision(2) << tripMileage << " MPG" << endl;
	cout << "Trip Cost:             $" << fixed << showpoint << setprecision(2) << TripCost << endl;
	cout << "Trip cost per mile:    $" << fixed << showpoint << setprecision(2) << CostPerMile << endl;
	
}


void showTotals (double totalMiles, double totalGallons, double totalCost, double overallMPG)
{

	cout << "Total Miles:     " << totalMiles << endl;
	cout << "Total Gallons:     " << totalGallons << endl;
	cout << "Total Cost:     " << totalCost << endl;
	cout << "Overall MPG:     " << overallMPG << endl;
}

void showMileageComparison (double overallMPG)
{

	double MileageComp = overallMPG - FleetAvg;

		if (MileageComp > 0)
			{
				cout <<	 "Your vehicle's mileage is greater than the fleet average by " << MileageComp << "mpg.";
			}
		else if (MileageComp == 0)
			{
				cout << "Your vehicle's mileage is equal to the fleet average of " << FleetAvg << "mpg.";
			}
		else if (MileageComp < 0)
			{
				cout << "Your vehicle's mileage is is less than the fleet average by " << MileageComp << "mpg.";
			}
}


Last edited on
@Joshua

Just a small style point:

If you are going to use do-while loops, put the while statement on the same line as the closing brace of the do, like this:

1
2

}  while((ANSWER == 'Y')||(ANSWER == 'y'));


The problem is that it can look like a while loop with a null statement.

I prefer not to use a do loop if I can help it, use for or while instead. All 3 forms of loops are normally interchangable ( one form can be rewritten in another form).

Also, you can use the C function toupper, so that you don't have a double test ( ie Y or y). There is a C++ function that does the same thing if you use a <string> instead of a char.

Finally, it's agood idea to initialise your variables when you declare them, uninitialised variables are one of the main causes of errors in programming.

You have used really good variable names ( which I like), however I always write a comment to describe what it is about with other info say, max min values allowed. So my mantra is "declare initialise & comment on one line"

It is also quite a good idea to show an example calculation using comments - this really helps anyone else reading the code. All this stuff might seem over the top for this simple example, but these concepts wil help make you a better coder.

I like your work - Good stuff.
@TheIdeasMan

Thanks a lot!! I figured out what I was doing wrong and added the tips you suggested to clearify my program. Also THANK YOU to everyone else who posted and helped with my assignment!!
@Joshua,

Another thing I noticed:


1
2
3
4
5
6
7
8
9
10
11
12
13
if (MileageComp > 0)
			{
				cout <<	 "Your vehicle's mileage is greater than the fleet average by " << MileageComp << "mpg.";
			}
		else if (MileageComp == 0)
			{
				cout << "Your vehicle's mileage is equal to the fleet average of " << FleetAvg << "mpg.";
			}
		else if (MileageComp < 0)
			{
				cout << "Your vehicle's mileage is is less than the fleet average by " << MileageComp << "mpg.";
			}


You have to be carefull comparing doubles, you can't do it like you have.

Because of the binary representation of floats & doubles, there are a lot of numbers that can't be represented exactly. The number 0.1 is one of them so this doesn't work as you might expect:

1
2
3
4
5
6
7
8
9
double MyDouble = 0.1;   //Used to demonstrate problems with floating point

if(1.0 == 10 * MyDouble  ) {
          cout  << "Exact"  << endl;   // always fails
           }
          else {
           cout  << "Not Exact"  << endl;   // always true}
}


The braces aren't necessary in this example, but I have them as a defensive measure - If I add more code later this might save me.

One way to do this is to make a comparison between an absolute value and a value that represents the precision you want:

1
2
3
4
5
6
7
8
9
10
11

const double MileagePrecision = 0.01;  //Precision of mileage answers anything
                                                         // less than this is zero

if (abs(MileageComp ) < MileagePrecision ) {
           cout  << "MileageComp is zero"  << endl;   //
           }
else  {
           //whatever code you want here
}


You should google floating point and DBL_EPSILON to read all about the tech details. There are articles on this site.

HTH Hope This Helps
Topic archived. No new replies allowed.