Your job is to write a program which will help a person make change. You
will get the cost of an item from the user, then show them the tax and
total cost (cost+tax), then ask them for how much they are paying with,
and finally show the user how much change they will get (amount paid
minus total cost). We will use a constant value of 0.0625 for the tax.
It would be nice to have a line in your code like this:
For an added challenge, try to
format your output so that it only shows dollars and cents. To do this,
you could include the iomanip library:
const float tax = 0.0625;
Then whenever you need to do a calculation, you can use the variable called tax
instead of that big awkward number. Note that it is a constant so no
one can accidentally change it to another value--makes for safer code.What is the cost of the item? 7.95 Tax: 0.496875 Total: 8.446875 How much money? 10.00 Your change is 1.553125 |
#include <iomanip>There are output manipulators like setw, left, right, and setprecision which might help--or not. After you look up what the manipulators will do and experiment for an hour or so, you will likely believe that whoever created them was insane and didn't actually want you to be able to format anything.You could make a function like this which would round to the nearest 100th:
float round(float number) {
return (int)(number*100.0 + 0.5)/100.0;
}
0 comments:
Post a Comment