Back to Challenge Set 2

Code Challenge: Fuel Calculator

Extra CreditOptional. Not required for any homework drop.

Difficulty  

divider

The Brief

What they asked for
I am driving to my cousin's this weekend and I want to know
what the gas is going to run me before I commit. My car is
pretty good on gas but I do not remember the exact number,
so I would rather type it in each time than have it built in.
One thing though: the pump rounds. I buy 13.1 gallons, not
13.125 gallons, and the price I actually pay is based on the
rounded number. Give me a total that matches what my receipt
would actually say.

Write your requirements list before you write any code. Input, calculate, output, and anything you had to decide for yourself.


Notes

  • Three things have to come from the person running the program, not from you.
  • Every one of them needs int.Parse or double.Parse before you can do arithmetic with it.
  • Gas is sold by the gallon and the car is measured in miles per gallon. Work out how many gallons the trip needs before you round anything.
  • Round the gallons to one decimal place before you calculate the cost, not after.
  • This is not the same as printing a rounded number at the end. Prove it: 420 miles at 32 mpg and $3.49 a gallon comes to $45.72 one way and $45.81 the other — same inputs, nine cents apart, depending only on where the rounding happens.
  • .ToString("F1") returns text, not a number. If you multiply that text by a price and get something strange — or it refuses to compile at all — that is this note, arriving early.
  • If your total comes out to $45.81, you rounded after multiplying instead of before. That is the whole bug, self-checking.

Sample Output

Sample Output
How far are you driving (miles)? 420
What is your car's mpg? 32
Price per gallon? 3.49
Gallons needed: 13.125
Gallons (rounded): 13.1
Trip cost: $45.72

Round after multiplying instead of before and the same inputs give 45.81 — verified in a real run. That nine-cent difference, and where it comes from, is the whole point of this challenge.


Submit

Your requirements list and your program file, named cc-fuel-calculator.

Commence Challenge