Thread: trouble with passing adresses and calling on a function...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    21

    trouble with passing adresses and calling on a function...

    im taking c programming and i have to get my program to say how many quartes dimes nickels and pennies are in a certain value... 3 different times... i get the display to look right, but the correct values never come across... can anyone help... here is what i have so far.
    Any help would be appreciated i have been stuck for a few days on this...
    Code:
    #include <stdio.h>
    
    int main()
    {
    	void change(float, int *, int *, int *, int *); /*Prototype*/
    
    	int quarters, dimes, nickels, pennies;
           float firstnum = 1.88;
    	float secnum = 0.32;
    	float thirdnum;
    	
    
    	change(firstnum, &quarters, &dimes, &nickels, &pennies);/*Call to the Function*/
    	printf("\nTOTAL VALUE ENTERED: $%f", firstnum);
    	printf("\n%d Quarters", quarters);
    	printf("\n%d Dimes", dimes);
    	printf("\n%d Nickels", nickels);
    	printf("\n%d Pennies\n", pennies);
    
    
    	change(secnum, &quarters, &dimes, &nickels, &pennies);/*Call to the Function*/
    	printf("\nTOTAL VALUE ENTERED: $%f", secnum);
    	printf("\n%d Quarters", quarters);
    	printf("\n%d Dimes", dimes);
    	printf("\n%d Nickels", nickels);
    	printf("\n%d Pennies\n", pennies);
    
    	printf("\n Enter amount of money to display in change format:$ ");
    	scanf("%f", &thirdnum);
    	
    
    	change(thirdnum, &quarters, &dimes, &nickels, &pennies);/*Call to the Function*/
    	printf("\nTOTAL VALUE ENTERED: $%f", thirdnum);
    	printf("\n%d Quarters", quarters);
    	printf("\n%d Dimes", dimes);
    	printf("\n%d Nickels", nickels);
    	printf("\n%d Pennies\n\n", pennies);
    	
    	return 0;
    }
    
    
    void change(float money, int *quarters, int *dimes, int *nickels, int *pennies)
    {
    	
    
    
    	while(money>=0.25)
    	{
    		money-=0.25;
    		(*quarters++);
    		
    	}
    	while(money>=0.1)
    	{
    		
                  money-=0.1;
    		(*dimes++);
    		
    	}
    	while(money>=0.05)
    	{
    		money-=0.05;
    		(*nickels++);
    			
    	}
    
    	while(money>=0.01)
    	{
    		
    		money-=0.01;
    		(*pennies++);		
    	
    	}
    	
    }

  2. #2
    Massively Single Player AverageSoftware's Avatar
    Join Date
    May 2007
    Location
    Buffalo, NY
    Posts
    141
    Postfix ++ has precedence over *.

    You need:
    Code:
    (*quarters)++
    And so on. As it is now, you're incrementing the pointer, not the value.

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    21

    changed )++

    After the change the values are still way out to lunch. Something still is not right.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Post up your revised code, please.
    Last edited by Adak; 06-01-2007 at 06:53 PM.

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    21

    Money input...

    Code:
    #include <stdio.h>
    
    int main()
    {
    	void change(float, int *, int *, int *, int *); /*Prototype*/
    
    	int quarters, dimes, nickels, pennies;
           float firstnum = 1.88;
    	float secnum = 0.32;
    	float thirdnum;
    	
    
    	change(firstnum, &quarters, &dimes, &nickels, &pennies);/*Call to the Function*/
    	printf("\nTOTAL VALUE ENTERED: $%f", firstnum);
    	printf("\n%d Quarters", quarters);
    	printf("\n%d Dimes", dimes);
    	printf("\n%d Nickels", nickels);
    	printf("\n%d Pennies\n", pennies);
    
    
    	change(secnum, &quarters, &dimes, &nickels, &pennies);/*Call to the Function*/
    	printf("\nTOTAL VALUE ENTERED: $%f", secnum);
    	printf("\n%d Quarters", quarters);
    	printf("\n%d Dimes", dimes);
    	printf("\n%d Nickels", nickels);
    	printf("\n%d Pennies\n", pennies);
    
    	printf("\n Enter amount of money to display in change format:$ ");
    	scanf("%f", &thirdnum);
    	
    
    	change(thirdnum, &quarters, &dimes, &nickels, &pennies);/*Call to the Function*/
    	printf("\nTOTAL VALUE ENTERED: $%f", thirdnum);
    	printf("\n%d Quarters", quarters);
    	printf("\n%d Dimes", dimes);
    	printf("\n%d Nickels", nickels);
    	printf("\n%d Pennies\n\n", pennies);
    	
    	return 0;
    }
    
    
    void change(float money, int *quarters, int *dimes, int *nickels, int *pennies)
    {
    	
    
    
    	while(money>=0.25)
    	{
    		money-=0.25;
    		(*quarters)++;
    		
    	}
    	while(money>=0.1)
    	{
    		
                  money-=0.1;
    		(*dimes)++;
    		
    	}
    	while(money>=0.05)
    	{
    		money-=0.05;
    		(*nickels)++;
    			
    	}
    
    	while(money>=0.01)
    	{
    		
    		money-=0.01;
    		(*pennies)++;		
    	
    	}
    	
    }
    So is the change function not getting the value of money to be changed over into coins? With fistnum secnum and thirdnum being in the call to the function does it not take care of that?

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Initialize quarters, dimes, nickels, and pennies to 0 at the start of the program.

  7. #7
    Registered User
    Join Date
    Jun 2007
    Posts
    21
    See it is something so simple that makes it seem so hard... thanks alot! It works like a champ now. Just so you know i tried a long time before i came for help... i appreciate it.

Popular pages Recent additions subscribe to a feed