making change in dollars project question

Hello I'm working on this problem where the user inputs an amount of money between $0.00 and $19.99, and the program outputs the best way to calculate the change of that amount. So for example, if I put in "$9.94", then the program should output

One $5 bill
4 $1 bills
3 quarters
1 dime
1 nickel
4 pennies

I started doing this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
float x;
cout << "Input a dollar amount between 0.00 and 19.99 inclusive: ";
cin >> x;

cout << "Change of " << x << " is best given as: " << endl;

if (x > 10.00)
{
  cout << "One $10 bill" << endl;
}

else if (5.00 < x && x <= 9.99)
{
  cout << "Two $5 bills" << endl;
}

else if (0.00 < x && <= 5.00)
{
  cout << "Three $5 bills" << endl;
}


but I feel like its really inefficient and am wondering if you guys can point me in the right direction as to how to make this program. I can't think of a way to use loops in this, but if there is a way, will you let me know?

Thanks for reading


Yes that is very inefficient.. and it will require much more coding than a simpler alternative.

1) Use variables. Make a variable that counts how many of each bill/coin you need.

2) Use division (/) and mod (%) operators to easily find out how many coins you need. Note that mod only works with integers, so I could multiply by 100 to get rid of that floating point.

hint:

This should require exactly zero if statements. You should be able to do it all with math.
Last edited on
ok thanks

how would you implement the multiplying 100 part? Like for example, if I wanted to find the number of 10 dollar bills it had to use,

tenDollar = total*100 % 10*100

like that?
Last edited on
I suggest writing a class that stores Money in ints.

class Money
{
int mDollars;
unsigned int mCents;

// then fill in the required methods for manipulating this data.
};
Topic archived. No new replies allowed.