At wits end!

Everything in my program is working except I need it to calculate the Total MPG of both tankfuls of gas and it is not ending on the sentinel. I am 2 months new to programming and need some help. Here is the program:

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

using namespace std;

int main()
{double miles(0), gallons(0), MPG(0), TotalMPG_1(0);

while(miles!=(-1))
{cout << "Enter the miles used(-1 to quit):" << endl;
cin >> miles;

cout << "Enter gallons:" << endl;
cin >> gallons;

MPG = (miles/gallons);

cout << setprecision(6) << "MPG this tankful: " << MPG << endl;

TotalMPG_1 = (miles+miles)/(gallons+gallons);
cout << "Total MPG: " << setprecision(6) << TotalMPG_1 << endl;}
return 0;

}

Can anyone help a newbie?
while(miles!=(-1))

Floating points are approximations. It is doubtful you will ever get exactly -1. Even if you input -1, 'miles' will probably actually contain something like -1.000000001 or -0.999999999.

Therefore you should never really use == or != when comparing floating point values.

Possible solutions:

1) check for any negative value rather than -1 specifically (ie: while(miles >= 0))

2) don't use a floating point for 'miles', make it an int instead (but then, of course, the user won't be able to input a floating point)
I tried that and the user cannot put in a floating point. Not only that the MPG and Total MPG outputs int values too. Plus the program still doesn't end when the user inputs a negative value.
hy,
I think you should accept all values first to step into while
then compare the input!

1
2
3
4
5
6
while(true) {
       cout << "Enter the miles used(-1 to quit):" << endl;
       cin >> miles;
       if (miles < 0) break;
       /* rest of the code*/
}
Last edited on
Who is that kid? Oh it is codekiddy:) Thanks brother! That worked! I hate to keep hassling you but do you see why my program will not calculate the Total MPG?
Not enough variables, IMO ..
Try this way..
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
// MPG.cpp : Defines the entry point for the console application.
//

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

using namespace std;

int main()
{
	double miles, gallons, MPG, TotalMPG_1,TotalMiles,TotalGallons;
	miles=0.0;
	TotalMiles = 0.0;
	gallons = 0.0;
	TotalGallons = 0.0;
	TotalMPG_1 = 0.0;
	while(miles >= 0)
	{
		cout << "Enter the miles driven (-1 to quit):" << endl;
		cin >> miles;
		TotalMiles+=miles;
		if ( miles >=0.0)
		{
		cout << "Enter gallons:" << endl;
		cin >> gallons;
		TotalGallons+=gallons;
		MPG = (miles/gallons);
		
		cout << setprecision(6) << "MPG this tankful: " << MPG << endl;

		TotalMPG_1= TotalMiles/TotalGallons;
		cout << "Total MPG: " << setprecision(6) << TotalMPG_1 << endl << endl;
		}
	}
return 0;
}
Last edited on
Thanks whitenite1!
I hate to keep hassling you but do you see why my program will not calculate the Total MPG?
you can't calculate MPG in cases where miles and galons bay contain zeros!!
dividing with zeros is inpossilbe!
and you have set them to zero as default value.
1
2
3
4
5
if (miles != 0 && galons != 0)
         MPG = (miles/gallons);
else
        MPG = 0;
/*rest of the code*/


use the code from whitenite1
cheers!
Topic archived. No new replies allowed.