need help making change calculator

Ok, I am fairly new to C++ programming and I need to make a program that asks the user to cout<<"Enter an of change (in cents): "; and then the program will calculate the amount of change in quarters, dimes, nickels, and pennies. I can't even figure out a way to start this program otherwise I would have posted some type of progress on it. Should I have three different void functions that I call in the int main? If anyone can help me get through this I would really appreciate it.
closed account (o3hC5Di1)
Hi there,

I believe this thread will have all the information you need: http://www.cplusplus.com/forum/beginner/43970/

All the best,
NwN
I used the thread you posted as some type of a guide to get this started but I am having troubles in the computations. I am getting weird numbers as much answers and I need the extras to roll over.. i.e. 5 pennies would be 1 nickel and 2 nickels would roll over to a dime. I may need to use the modulous operator?

here is what i have so far:

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
#include <iostream>

using namespace std;

int main()

{
    int change;
    cout<<"Enter the amount of change (in cents): ";
    cin>>change;
    
    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;
    
    cout<<"quarters=  "<<quarters<<endl;
     
    cout<<"dimes=  "<<dimes<<endl;

    cout<<"nickels=  "<<nickels<<endl;
        
    cout<<  "cents=  "<<cents<<endl;
    
    system("pause");
    return 0;
}
If anyone can help me correct this, I would greatly appreciate it.
closed account (o3hC5Di1)
Hi there,

You are making a common beginner mistake: When you divide integers, an integer will be returned and any floating point values will be clipped. I suggest you use floating point datatypes to handle currencies. I've also made a few more changes to your code:

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
#include <iostream>

using namespace std;

const double QUARTER_AMOUNT = 0.25F;
const double DIME_AMOUNT = 0.10F;
const double NICKEL_AMOUNT = 0.05F;
const double CENT_AMOUNT = 0.01F;

int main()

{
    double change; //make a double
    cout<<"Enter the amount of change (in cents): ";
    cin>>change;
    
    double quarters  =  change / QUARTER_AMOUNT;
    change = change - quarters;
    
    double dimes = change / DIME_AMOUNT;
    change = change - dimes;
    
    double nickels = change / NICKEL_AMOUNT;
    change = change - nickels;
    
    double cents = change / CENT_AMOUNT;
    
    cout<<"quarters=  "<<quarters<<endl;
     
    cout<<"dimes=  "<<dimes<<endl;

    cout<<"nickels=  "<<nickels<<endl;
        
    cout<<  "cents=  "<<cents<<endl;
    
    system("pause");
    return 0;
}


Using constants for fixed values makes your code usually more readable than using "magic numbers" (numbers in the code that may not mean much to someone not familiar with the program).

Also, please note that we try to discourage the use of system() because it holds safety risks. For alternative methods of preventing the console closing down, please see: http://www.cplusplus.com/forum/beginner/1988/

All the best,
NwN
I tried using your changed code that you have provided and it is still giving me some crazy numbers. For example when I input 41 (cents) I get;

1
2
3
4
quarters= 164
dimes= -1230
nickels= 22140
cents= -2.1033e+006


None of which are right obviously.

Thank you as well for the information regarding the system(). I will keep that in mind. Looking forward to a reply.
Last edited on
closed account (o3hC5Di1)
Ah yes, I forgot your input was in cents, you need to change:

1
2
3
4
const double QUARTER_AMOUNT = 25;
const double DIME_AMOUNT = 10;
const double NICKEL_AMOUNT = 5;
const double CENT_AMOUNT = 1


You could also make those variables int's again if you like.

All the best,
NwN
Yes, I see where they fixes my major problem of the impossible numbers. However, it does calculate the amount of change for each denomination. How would I get it to roll over.

For example:
User inputs: 41 cents

Program outputs: one quarter, one dime, one nickel, one penny.

I don't want this program to tell me how many of each denomination I can have to make my total.

Thanks again for the help, I believe I am getting closer to the outcome haha.
Does anyone have any ideas on how to get the outcomes to roll over in my change? i.e. 5 pennies would be 1 nickel and 2 nickels would roll over to a dime.

Thanks
1
2
3
4
5
6
7
8
9
10
    unsigned int numQuarters = amountInCents / QUARTER_AMT;  //here's how many whole quarters I can take out
    amountInCents -= (numQuarters * QUARTER_AMT);  //if I take out that many quarters, here's what I'm left with to work on

    unsigned int numDimes = amountInCents / DIME_AMT;
    amountInCents -= (numDimes * DIME_AMT);

    unsigned int numNickels = amountInCents / NICKEL_AMT;
    amountInCents -= (numNickels * NICKEL_AMT);

    unsigned int numPennies = amountInCents;


amountInCents is what you start with from the user. As you go along, decrement this total by however much change you've translated into coin amounts.
Last edited on
Thanks booradley60!

That solved my carry over problem, I've never used unsigned int before but I integrated into my code and it made it run perfectly.

Thanks again.
Topic archived. No new replies allowed.