Makin' Change

I've have a project due this week and I'm having trouble with a specific section of my code. The exercise is titled "MakeChange" and I am to design it so that it makes the least amount of change for a dollar value entered. I'm pretty sure I have 90% of the code done correctly but I can't figure out how to have the change done correctly. Basically after it takes out the whole dollar amounts, I can't figure out how to have the following lines of code remove the previous amount.

Example: Make change for $9.23

Dollars= 9
Half Dollars= 0
Quarters= 0
Dimes= 2
Nickels= 0
Pennies= 3

I can get the dollars to compute correctly but non of the remaining denominations are correct.

It's lines 24-30:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Make Change Exercise

#include <iostream>
using namespace std;

// Step 1: Prompt user to enter dollar value
// Step 2: Assign dollar value variables
// Step 3: Return change in full dollars
// Step 4: Return change in half dollars
// Step 5: Return change in quarters
// Step 6: Return change in dimes
// Step 7: Return change in nickels
// Step 8: Return change in cents

// End of Algorithm

int main ()
{
    int change;
    cout << "How much change would you back?  "  "Enter dollar value:  ";
    cin  >>  change;
    
    
    int dollar = change / 1;
    int halfdollar = dollar / .50;
    int quarters  =  halfdollar / .25;
    int dimes = quarters / .10;
    int nickels = dimes / .05;
    int cents = nickels / .01;
    

    
    //Step 2:  Return change in full dollars
    
    cout  <<  "The # of dollars=  "  <<  dollar  << endl;
    
    //Step 3: Return change in half dollars
    
    cout  <<  "The # of half dollars=  "  <<  halfdollar  << endl;
    
    //Step 4:  Return change in quarters
    
    cout  <<  "The # of quarters=  "  <<  quarters  <<  endl;
    
    //Step 5:  Return change in dimes
    
    cout  <<  "The # of dimes=  "  <<  dimes  <<  endl;
    
    //Step 6:  Return change in nickels
    
    cout  <<  "The # of nickels=  "  << nickels  <<  endl;
    
    //Step 7:  Return change in cents
    
    cout  <<  "The # of cents=  "  <<  cents  <<  endl;
    
    
    
    system("pause");
    return 0;
    
}


Any help would be greatly appreciated.

- Ray
You left out the two-dollar bill :)

The number of half dollars to output is being calculated wrongly.

Here, for example, you are calculating the number of half-dollars in the amount of dollars you have given back as change. You should be calculating the number of half-dollars in the change yet to be given back.
int halfdollar = dollar / .50;

The correct calculation is the amount of change minus the number of dollars already calculated, divided by 0.5 and rounded down to an integer.

Similarly for all the others.

I suggest that after each calculation, you recalculate change, subtracting the amount of change you've already dealt with.

Something like:

1
2
int dollar = change / 1;
change = change - dollar;
Last edited on
I forgot about the two dollar bills... Haha!

I appreciate your feedback and have updated my code. However it's still not computing everything correctly. Here's what the code now looks like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int main ()
{
    double change;
    cout << "How much change would you back?  "  "Enter dollar value:  ";
    cin  >>  change;
    
    
    int dollar = change / 1;
    change = change - dollar;
    
    int halfdollar = change / .50;
    change = change - halfdollar;
    
    int quarters  =  change / .25;
    change = change - quarters;
    
    int dimes = change / .10;
    change = change - dimes;
    
    int nickels = change / .05;
    change = change - nickels;
    
    int cents = change / .01;


When I enter $9.78 for it compute, here's what I get back:

Dollars= 9 (great start!)
hakf dollars= 1 (nice!)
quarters= 0 (uh-oh)
dimes = -2 (huh?)
nickels = 35
cents = -3321



Secondly on line 3 I have it listed as a "double" but then as an "int" throught the rest of the code. When I compile it I get a "[Warning:] converting to 'int' from 'double'". Is this just a warning or can I not do this?
Topic archived. No new replies allowed.