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:
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.
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?
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*/