Functions

This is a discussion on Functions within the C Programming forums, part of the General Programming Boards category; I am struggling horribly with functions and I am wondering if anyone can tell me why the following code does ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    4

    Functions

    I am struggling horribly with functions and I am wondering if anyone can tell me why the following code does not display the value of dollar correctly. I have commented out the middle bit as I have know idea how to get that part going as a function.

    Code:
    #include <stdio.h>
    
    void GetData(float &change)
    {
    	printf("Using intervals of five for the cents, please enter any amount of dollars and cents: $");
    	scanf("%f%*c", &change);
    	return;
    }
    
    void SplitChange(int &dollar, int &cents)
    {
    	int change;
    	dollar = (int) change;
    	cents = (int) (((change - dollar)*100) + 0.5);
    	return;
    }
    
    int main()
    
    {
    	float change;
    	int dollar, cents;
    	const int ONEHUNDREDDOLLARS = 100, FIFTYDOLLARS = 50, TWENTYDOLLARS = 20,
    		TENDOLLARS = 10, FIVEDOLLARS = 5, TWODOLLARS = 2, ONEDOLLAR = 1, FIFTYCENTS = 50,
     		TWENTYCENTS = 20, TENCENTS = 10, FIVECENTS = 5;
    	
    	GetData(change);
    	SplitChange(dollar, cents);
    		
    	/*printf("The notes and coins required to make change for %f\n dollars and cents are: ", change);		
    
    		while(dollar >= ONEHUNDREDDOLLARS) 
    		{
    		printf("%d dollars ", ONEHUNDREDDOLLARS); 
    		dollar -= ONEHUNDREDDOLLARS;
    		if(dollar > 0)
    			printf(", ");
    		}
    		
    		if(dollar >= FIFTYDOLLARS) 
    		{
    		printf("%d dollars ", FIFTYDOLLARS); 
    		dollar -= FIFTYDOLLARS;
    		if(dollar > 0)
    			printf(", ");
    		}
    	
    		while(dollar >= TWENTYDOLLARS) 
    		{
    		printf("%d dollars ", TWENTYDOLLARS); 
    		dollar -= TWENTYDOLLARS;
    		if(dollar > 0)
    			printf(", ");
    		}
    
    		while(dollar >= TENDOLLARS) 
    		{
    		printf("%d dollars ", TENDOLLARS); 
    		dollar -= TENDOLLARS;
    		if(dollar > 0)
    			printf(", ");
    		}
    		
    		if(dollar >= FIVEDOLLARS) 
    		{
    		printf("%d dollars ", FIVEDOLLARS); 
    		dollar -= FIVEDOLLARS;
    		if(dollar > 0)
    			printf(", ");
    		}
    
    		while(dollar >= TWODOLLARS) 
    		{
    		printf("%d dollars ", TWODOLLARS); 
    		dollar -= TWODOLLARS;
    		if(dollar > 0)
    			printf(", ");
    		}
    
    		if(dollar >= ONEDOLLAR) 
    		{
    		printf("%d dollar ", ONEDOLLAR); 
    		dollar -= ONEDOLLAR;
    		if(dollar > 0)
    			printf(", ");
    		}
    
    		if(cents >= FIFTYCENTS) 
    		{
    		printf("%d cents ", FIFTYCENTS); 
    		cents -= FIFTYCENTS;
    		if(cents > 0)
    			printf(", ");
    		}
    	
    		while(cents >= TWENTYCENTS) 
    		{
    		printf("%d cents ", TWENTYCENTS); 
    		cents -= TWENTYCENTS;
    		if(cents > 0)
    			printf(", ");
    		}
    	
    		if(cents >= TENCENTS) 
    		{
    		printf("%d cents ", TENCENTS); 
    		cents -= TENCENTS;
    		if(cents > 0)
    			printf(", ");
    		}
    
    		if(cents >= FIVECENTS) 
    		{
    		printf("%d cents ", FIVECENTS); 
    		cents -= FIVECENTS;
    		if(cents > 0)
    			printf(", ");
    		}*/
    	printf("%d dollar ", dollar);
    	
    
    	return(0);
    }
    Thanks heaps
    Last edited by Salem; 09-17-2006 at 01:02 AM. Reason: Fixed [code][/code] tags.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Use [code] tags before Quzah reads that! Quick! (It's [code], not <code>.)

    [edit] Okay, Dave_Sinkula did it for you. Now I can read it.
    Code:
    void SplitChange(int &dollar, int &cents)
    That looks like C++ to me . . . Dave . . .
    Code:
    void SplitChange(int &dollar, int &cents)
    {
    	int change;
    	dollar = (int) change;
    	cents = (int) (((change - dollar)*100) + 0.5);
    	return;
    }
    That has a serious problem with an uninitialized variable.
    [edit=2]
    I'm guessing you want something like this, assuming change is the total number of cents involved:
    Code:
    void SplitChange(int change, int &dollar, int &cents)
    {
    	dollar = change / 100;
    	cents = change % 100;
    	return;
    }
    [/edit][/edit]
    Last edited by dwks; 09-16-2006 at 08:52 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 02:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM

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