

all 8 comments

[–]enterthebored 2 points3 points  (2 children)

amount = 2.50 + (miles-5)*0.75;

[–]redsblue[S] 0 points1 point  (1 child)

Obviously. I could work the problem out in my head, but I felt like I was missing something in the code. That's what I get for cramming.. Thank you.

[–]enterthebored 2 points3 points  (0 children)

Sometimes you look at a problem "too long". Glad to help.

[–]ladaghini 1 point2 points  (3 children)

For every mile up to 5 miles walked, you're paid .50 cents. So, 5 miles nets you $2.50.

You should look up something called Verizon Math.

[–]conversionbot -1 points0 points  (2 children)

5 miles = 8.05 kilometers

[–]Baker590 0 points1 point  (1 child)

Thanks Conversionbot!

[–]conversionbot 1 point2 points  (0 children)

Don't sweat it, bro!

[–]ladaghini 1 point2 points  (0 children)

I just wanted to point out that you ought to use a unit of currency that allows you to express money in integers. It just happens that in this case, you can express the rates in cents per mile, since both $0.50 and $0.75 can be expressed in cents using ints.

The reason for this is that floating point numbers don't always represent the intended data perfectly and precisely in binary. (Incidentally, both 0.5 and 0.75 can be represented perfectly in binary, but that's not always the case; had the rates been 0.4 and 0.8, then the floating point representation wouldn't be exact.)

So, I'd insist you let amt (which I presume means amount) represent the amount in cents. So you'd so something like

amt = 250 + (miles - 5) * 75;

And then, when you need to print the amount in dollar and cents, if the amount is 325 cents, then 325 / 100 == 4 which gives the whole dollar amount, and 325 % 100 == 25 which gives the cents remaining after counting the whole dollars. So, you can do this:

cout << "You are owed $" << amt / 100 << ".";

cout.width(2);  // Force two character output for cents, and
cout.fill('0'); // pad extra space with zeros, so 9 cents 
                // shows up after decimal point as 09 and 
                // not simply 9.
cout << amt % 100 << endl;