1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Help With Change Amount Program

Discussion in 'C++' started by rbreakey, Apr 15, 2011.

  1. rbreakey

    rbreakey New Member

    Joined:
    Apr 15, 2011
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    My program is producing '0's' for the amount of each denomination of dollars/coins. 100 dollars, 50 dollars, 20 dollars, 10 dollars, 5 dollars, 2 dollars, 1 dollars, 50 cents, 20 cents etc...

    Say if the user inputs: "102.80", It should produce output saying:
    100dollars: 1
    2dollars: 1
    50 cents: 1
    20 cents: 1
    10 cents 1

    Can anyone point out why it is producing 0's instead? Any help would be appreciated, thanks.

    Code:
    #include <stdio.h>
    
    int GetChange()
    {
    	float change;
    
    	printf("Please enter a change amount: ");
    	scanf("%d%*c", &change);
    
    	return (change);
    }
    
    int NumCents(int cents, int denomination)
    {
    	return (cents / denomination);
    }
    
    int RemainderCents(int cents, int denomination)
    {
    	return (cents % denomination);
    }
    
    int NumDollars(int dollar, int denomination)
    {
    	return (dollar / denomination);
    }
    
    int RemainderDollars(int dollar, int denomination)
    {
    	return (dollar % denomination);
    }
    
    void PrintChange(int n100dollars, int n50dollars, int n20dollars, int n10dollars, int n5dollars, int n2dollars, int n1dollars, int n50cents, int n20cents, int n10cents, int n5cents)
    {
    	printf("100dollars: %d\n50dollars: %d\n20dollars: %d\n10dollars: %d\n5dollars: %d\n2dollars: %d\n1dollars: %d\n50cents: %d\n20cents: %d\n10cents: %d\n5cents: %d\n", n100dollars, n50dollars, n20dollars, n10dollars, n5dollars, n2dollars, n1dollars, n50cents, n20cents, n10cents, n5cents);
    
    	return;
    }
    
    int main()
    {
    	float change;
    	int dollar, n100dollars, n50dollars, n20dollars, n10dollars, n5dollars, n2dollars, n1dollars;
    	int cents, n50cents, n20cents, n10cents, n5cents;
    
    	change = GetChange();
    	dollar = (int) change;
    	cents = (int) (((change - dollar)*100) + 0.5);
    	
    	n100dollars = NumDollars(dollar, 100);
    	dollar = RemainderDollars(dollar, 100);
    	
    	n50dollars = NumDollars(dollar, 50);
    	dollar = RemainderDollars(dollar, 50);
    	
    	n20dollars = NumDollars(dollar, 20);
    	dollar = RemainderDollars(dollar, 20);
    	
    	n10dollars = NumDollars(dollar, 10);
    	dollar = RemainderDollars(dollar, 10);
    	
    	n5dollars = NumDollars(dollar, 5);
    	dollar = RemainderDollars(dollar, 5);
    	
    	n2dollars = NumDollars(dollar, 2);
    	dollar = RemainderDollars(dollar, 2);
    	
    	n1dollars = NumDollars(dollar, 1);
    
    	n50cents = NumCents(cents, 50);
    	cents = RemainderCents(cents, 50);
    
    	n20cents = NumCents(cents, 20);
    	cents = RemainderCents(cents, 20);
    
    	n10cents = NumCents(cents, 10);
    	cents = RemainderCents(cents, 10);
    
    	n5cents = NumCents(cents, 5);
    
    	PrintChange(n100dollars, n50dollars, n20dollars, n10dollars, n5dollars, n2dollars, n1dollars, n50cents, n20cents, n10cents, n5cents);
    
    	return(0);
    }
    
     
  2. rbreakey

    rbreakey New Member

    Joined:
    Apr 15, 2011
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Nevermind, i've solved it.

    In the GetChange function i was scanning in a 'decimal' when it's meant to be a 'float', "%f%*c".

    Haha, oh dear.
     

Share This Page