First off, I'm a complete and absolute programming newbie and a bit of an old fart to boot, so I'd appreciate a gentle-but-firm correction if I'm pushing the wrong buttons here.
I'm slowly working through a used copy of Prata's C++ 5th edition, but something's not working in my version of one of the early chapter challenges. Maybe I've just been staring at it too long (I've rewritten this snippet from scratch twice), but I can't figure what's going wrong, and I'm spooked to try to pare this down for a forum post, so here goes.
// mpg.cpp -- A converter to change European fuel efficiency
// numbers over to American standards.
#include <iostream>
int main()
{
using namespace std;
const float KM_MI = 1.609; // km per mile
const float L_G = 0.2642; // liters per gallon
float mi, gal, mpg; // American
float l, km, l_km; // continental
cout << "The km driven? ";
cin >> km;
cout << "...and the liters used? ";
cin >> l;
l_km = l / (km / 100);
cout << "That's " << l_km << " liters per 100 km. \n";
mi = KM_MI * km;
gal = L_G * l;
mpg = mi / gal; // ???
cout << "Or, " << mi << " miles using ";
cout << gal << " gallons of fuel. \nThat's a total of ";
cout << mpg << " miles per gallon.";
return 0;
}
This returns...
The km driven? 100
...and the liters used? 12.4
That's 12.4 liters per 100 km.
Or, 160.9 miles using 3.27608 gallons of fuel.
That's a total of 49.1136 miles per gallon.
According to the book and a (supposedly functional) online calculator, 12.4 l/100km should return ~19 MPG. I must be fumbling my math somehow, but I can't get my head around it, since my miles and gallons seem to be converting properly, and the MPG equation is simply mi / gal, right?