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 |
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:
#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; }