help with calculating change from a dollar

This is a discussion on help with calculating change from a dollar within the C++ Programming forums, part of the General Programming Boards category; i know this is probably something really simple, but i just cant figure it out. here is the code. i ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    12

    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;
    Last edited by z.tron; 09-13-2002 at 01:25 PM.

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    13
    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)
    "Why not go mad?"

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    12
    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.

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    12
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to change static char* and not lose mem ?
    By jabka in forum C Programming
    Replies: 15
    Last Post: 09-07-2007, 05:33 PM
  2. Functions
    By thewizardalbany in forum C Programming
    Replies: 1
    Last Post: 09-16-2006, 08:46 PM
  3. Change Value in an array
    By beginner999 in forum C Programming
    Replies: 3
    Last Post: 01-18-2003, 06:16 AM
  4. Replies: 2
    Last Post: 11-08-2002, 02:22 AM
  5. Replies: 2
    Last Post: 09-04-2001, 02:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21