I just started my C++ course in college and I'm having major trouble
I have to write a program that dispenses changes.

It has to read the amount paid - amount purchased, and gives the change in dollars, dimes, etc

I need some help with this because I am so lost on what to do

any help will be appreciated

first you might want to start out with something simple like what the cost of something is and have them input it...then save that and have them input what they payed then create a simple formula for what the difference is then output it to the screen and so on.

I had to do something like this before I don't remember if I ever got it to work right or not I'll have to look tomorrow.


cout << "What is the price of the object that you are buying (in dollars)?\n\n";
cin >> amout_purchased;
cout << "What is the amount that you payed (in dollars)?\n\n";
cin >>amount_payed;
amount_purchased-amount_payed = difference;
cout <<"The amount of difference between what the price is of what you bought and what you payed is " << difference <<endl <<endl;

then calculate the amount of difference to dollars, quarters, dimes, etc.

Then calculate whole dollars from the difference then minus that from the difference then do the quarters, and so on...

amount_purchased-amount_payed = difference;

assignments should be done like this:

difference = amount_purchased-amount_payed;

it doesn't really matter it still ends up giving the same answer I was just doing a quick and sloppy program.

so when i want to transition from the number of of dollars dispensed,
do i use whatever i get from that then divide by modulus?

The modulus operator is routinely used on variables of type int. If the amount of the difference is a type float or double then you could multiply the difference by one hundred and cast it to an int. Then you can use integer math, including the % and /= operators, to determine the number of bills and coins to return. Integer math is uses all integers. That means that 10/3 is 3, not 3.333333333333333333 and 1/2 is 0 not 0.5, etc.

This article has been dead for over six months. Start a new discussion instead.