I think in all modern compiler
float
s are deprecated in favor of
double
s (offers more precision but uses more space, not a problem these days) so you can safely use
double
instead.
Also I would suggest to use a more up-to-date compiler since
conio.h
and
iostream.h
are not standard c++. It would help you create code more portable and efficient of new features.
Besides that, don't use
getch()
instead use
cin.get()
for the same purpose.
As regards your program itself be cautious with expression that detects real number equality/inequality like
gallons!=-1
Not a good idea at all. Unlike humans computers have some limited perception of numbers and they might create round off errors when calculating real numbers. In other words they cannot be trustworthy in those cases. For compiler 1 might be also equal to 1.000000000000000000001 since they only detect numbers up to a certain precision and latter number excess that precision.
Instead you can safely use conditions like
e < 0 //compares with 0 but not for equality
abs(gallons + 1) < e //where e is a small amount like 1e-5 = 10^-5. This one checks the absolute value (abs) of //gallons + 1 against 0 so basically checks gallons against -1
This latter although looks like relational condition but it virtually checks real numbers equality since it's used as a compensation for reduced compiler's precision.
You don't have to do implement this here since you just want to have an invalid number for loop exit. Use your negative numbers for this.
1 2
|
if(gallons==-1)
break;
|
CAUTION! For int you can safely use them in equality condition like:
if(a == -1)
As for the total for each if I am getting it right you can just use what @hamsterman said and keep them in an array. Just make sure you don't exceed your array's boundary (100 in your case). Maybe also you need to add a condition to check this also.
Something like
1 2 3 4 5 6
|
int N = 100, n = 0;
while(gallons!=-1 && n < N)
{
...
n++;
}
|