# help with calculating change from a dollar

• 09-13-2002
z.tron
help with calculating change from a dollar
i know this is probably something really simple, but i just cant figure it out. here is the code. i know i should be able to calculate it without using the newchange variable. any help is appreciated.
Code:

```#include <iostream> using namespace std; void main() {         // Programmer: Mike Gozdiskowski         // Date: 9/13/2002         // Discussion: An interactive program to calculate the change         // from a vending machine         // Input: Cost of item purchased (an integer between 0 and 100 inclusive)         // Output: Change is quarters, dimes, nickels, and cents         // Declaration and description of variables         long cost;        // Cost of item purchased in cents         long change;        // Amount of change in cents         long quarters;        // Number of quarters in change         long dimes;  //Number of dimes in change         long nickels;  //Number of nickels in change         long pennies;  //Number of pennies in change         long newchange;  //Variable used to store temporary amount of change for each coin         // Welcome user and get amount of purchase         cout<<"Welcome to the cash register!"<<endl;         cout<<"Please enter the amount of your purchase: ";         cin>>cost;         // Calculate the total change due         change=(100-cost);         // Calculate the number of quarters, dimes, and nickels in change         quarters=change/25;         newchange=change-quarters*25;         dimes=newchange/10;         newchange=newchange-dimes*10;         nickels=newchange/5;         newchange=newchange-nickels*5;         pennies=newchange/1;         // Print the results and exit program         cout<<"Total change due: "<<change<<" cents"<<endl;         cout<<"Your change is"<<endl;         cout<<"      "<<quarters<< "quarter(s)"<<endl;         cout<<"      "<<dimes<< "dime(s)"<<endl;         cout<<"      "<<nickels<< "nickel(s)"<<endl;         cout<<"      "<<pennies<< "pennie(s)"<<endl; }```

i did try something like this originally but i could not get it to work right so i just created a new variable. it would put the correct output only for some numbers.
Code:

```// Calculate the number of quarters, dimes, and nickels in change         quarters=change/25;         dimes=(change%25)/10;         nickels=(change%10)/5;         pennies=(change%5)/1;```
• 09-13-2002
I'm no expert, but asking myself how I'd do the same thing, this is what I got

Code:

```tempchange = change; while (tempchange > 0) {         if (tempchange > 25)         {tempchange = tempchange-25;         quarters=quarters+1;}         else if (tempchange > 10)         {tempchange = tempchange-10;         dimes=dimes+1;}         else if (tempchange > 5)                        {tempchange = tempchange-5;         nickels=nickels+1;}         else if (tempchange > 1)         {tempchange = tempchange-1;         pennies=pennies+1;} }```
I think this would work but if it doesn't or if it's grossly inefficient or something, feel free to let me know (I know you will ;o)
• 09-13-2002
z.tron
i can't use any loops or anything like that yet....it's just basic stuff.
i am looking for someone to help me out on where i went wrong with the example code at the bottom. the complete code i posted works fine, but i am pretty sure there is a more refine way of doing this problem.
• 09-13-2002
z.tron
ok, here is what i did:
Code:

```        // Calculate the number of quarters, dimes, and nickels in change         quarters=change/25;         change=change%25;         dimes=change/10;         change=change%10;         nickels=change/5;         change=change%5;         pennies=change/1;         change=(100-cost); //set change again otherwise it put garbage in output.```
that's what i was trying to figure out. i did try it this way before and i was confused because it was giving me the right amount of coins for how much money i put in, but it kept saying Total change due: 0
i reset change as you can see in the comment and it worked fine.
thanks for the help.