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?

mi = KM_MI * km;           
gal = L_G * l;

should probably be

mi = (1/KM_MI) *km;
gal = (1/L_G) *l;

because the units should cancel out in your multiplication
mi = (mi/km)*km = mi
gal = (gal/L)*L = gal

Unfortunately, that doesn't seem to do it. I THOUGHT I already handled the unit issue you mentioned; putting in your code gives me...

The km driven? 100
...and the liters used? 12.4
That's 12.4 liters per 100 km.
Or, 62.1504 miles using 46.9341 gallons of fuel.
That's a total of 1.3242 miles per gallon.

...which isn't the correct value for miles, gallons, or mpg. :(

Your head is wrapped around it wrong. I don't know how you got 19 mpg -- 160.9/3.27 is 49, not 19.

Your head is wrapped around it wrong. I don't know how you got 19 mpg -- 160.9/3.27 is 49, not 19.

I can't link you to the text I'm reading, but there are quite a few sites that offer liter/100km to mpg conversion, and they, too, give a 19 mpg figure for 12.4 L/100km. Somehow, they wind up with a conversion ratio of about 235, which I'm obviously not arriving at. Yes, I could make something that works in about 3 lines by using Wikipedia's conversion ratio, but the text I'm trying to learn from wants me to derive that number given the ratios. Besides, I don't think the problem is in that last division; it's somewhere earlier. I'm somehow screwing up what should amount to a straightforward conversion, and I'm utterly failing to see where.

I'm going to try it again from the top; there must be something I missed.

this is wrong: mi = KM_MI * km; "KM_MI" == km per mile. So if you already have km and want to go to miles, you should divide by this number, not multiply: mi = km / KM_MI ; Same error is in your liters->gallons conversion

this is wrong: mi = KM_MI * km; "KM_MI" == km per mile. So if you already have km and want to go to miles, you should divide by this number, not multiply: mi = km / KM_MI ; Same error is in your liters->gallons conversion

Hmm. Actually, I'm illiterate; the liter/gallon conversion was pushing the correct number (albeit backwards); the miles/km conversion was just as you said. Thank you very much! And, I intend to use more descriptive variable names in the future.

Let me ask, is this forum a good place to ask questions at this very low level, or should I head elsewhere (and if so, where)? If this was enough to beach me for hours, it's going to be a slow learning process...

H
Let me ask, is this forum a good place to ask questions at this very low level, or should I head elsewhere (and if so, where)? If this was enough to beach me for hours, it's going to be a slow learning process...

We (try to) help everyone here from beginner to pro. So stick around ;) We all started out at the bottom.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.