Thread: Output and Control

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    3

    Output and Control

    I've been working on a problem that it just boggling my mind. I need to be able to convert a given number into change (i.e. quarters, dimes, nickels, pennies.) so if i type in 3.50 i get 14 quarters, if i type in 3.45 i get 13 quarters and two dimes etc. what i have so far is each number individually (i.e 3.50 gets me 14 quarters, 35 dimes, 70 nickels, 350 pennies) the question i have is, how am i able to do this? this is what i have so far


    Code:
     #include <iostream>
    #include <iomanip>
    using namespace std;
    
    int main( )
    {
    
    	float amount;
    	float quarters;
    	float dimes;
    	float nickels;
    	float pennies;
    	
    	cout << "I will convert your money into change. \n\n";
    	cout << "Enter the amount:";
    	cin >> amount;
    
    
    	quarters = amount/.25f;
    	cout << "You recieve " << quarters << "Quarters.\n\n" ;
    	dimes = amount/.10f;
    	cout << "You recieve " << dimes << " Dimes.\n\n" ;
    	nickels = amount/.05f;
    	cout << "You recieve " << nickels << " Nickels.\n\n" ;
    	pennies = amount/.01f;
    	cout << "You recieve " << pennies << " Pennies.\n\n" ;
    	
    	return 0;
    }
    this code does each coinage individually, but i need them to be unified
    ive tried many different things so far, to no avail.
    Last edited by omfg pwned; 02-24-2005 at 05:15 AM.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    You must update your total amount after each conversion:
    Code:
    quarters = amount / .25f;
    amount -= quarters * .25f;
    
    dimes = amount / .10f;
    amount -= dimes * .1f;
    etc...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    3
    another problem... i guess it automatically rounds up? so u end up with 0 after u subtract teh quarters

  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    3
    ooh i got it

    Code:
     quarters = amount/.25f;
    	amount  -= quarters *.25f;
    	cout << "You recieve " << quarters << " Quarters.\n\n" ;
    	
    	dimes = amount/.10f;
    	cout << "You recieve " << dimes << " Dimes.\n\n" ;
    	amount  -= dimes *.10f;
    	
    	nickels = amount/.05f;
    	cout << "You recieve " << nickels << " Nickels.\n\n" ;
    	amount  -= nickels *.05f;
    	
    	pennies = amount/.01f;
    	amount  -= pennies *.01f;
    	cout << "You recieve " << pennies << " Pennies.\n\n" ;
    also note that i changed the type of variable from float to int, seeing as there is no such thing as half a quarter/dime/nickel/penny

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 06-05-2009, 09:42 AM
  2. Replies: 3
    Last Post: 02-29-2008, 01:29 PM
  3. Redirecting program output straight to an edit control
    By bennyandthejets in forum C++ Programming
    Replies: 5
    Last Post: 07-05-2004, 08:25 AM
  4. Control different DA output value!
    By Hunterhunter in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 03-13-2003, 12:11 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM