Thread: help with calculating change from a dollar

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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, 07:16 AM
  4. Replies: 2
    Last Post: 11-08-2002, 03:22 AM
  5. Replies: 2
    Last Post: 09-04-2001, 02:12 PM