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.
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?
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:
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/
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.
unsignedint 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
unsignedint numDimes = amountInCents / DIME_AMT;
amountInCents -= (numDimes * DIME_AMT);
unsignedint numNickels = amountInCents / NICKEL_AMT;
amountInCents -= (numNickels * NICKEL_AMT);
unsignedint 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.