Like Tree2Likes

Making a change counting program

This is a discussion on Making a change counting program within the C Programming forums, part of the General Programming Boards category; Write a C function named change() that accepts a floating point number of total coins and the addresses of the ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    7

    Making a change counting program

    Write a C function named change() that accepts a floating point number of total coins and the addresses of the integer variables named quarters, dimes, nickels, and pennies. The function should determine the number of quarters, dimes, nickels, and pennies in the total coins number passed to it and write these values directly into the respective variables declared in its calling function using pointers.

    With that being said, I am following the directions in the assignment, so don't tell me to write this 5 other ways.

    My issue is this. I have written the program, as shown below, and it all works EXCEPT that when I enter in a value of .24 and lower, the change count is incorrect.

    Code:
    #include <stdio.h>
    
    int change (float total, int *quarters, int *dimes, int *nickels, int *pennies);
    int main ()
    
     {
     int quarters, dimes, nickels, pennies;
     float total;
     {
    	total = 1.88;
            
        change (total, &quarters, &dimes, &nickels, &pennies);
            printf("TOTAL VALUE ENTERED: $%.2f", total);
            printf("\n%3d quarters\n", quarters);
            printf("\n%3d dimes\n", dimes);
            printf("\n%3d nickels\n", nickels);
            printf("\n%3d pennies\n", pennies);
     }
     {
    	total=0.32; 
               
        change (total, &quarters, &dimes, &nickels, &pennies); 
            printf("\nTOTAL VALUE ENTERED: $%.2f",total); 
            printf("\n%3d quarters\n",quarters); 
            printf("\n%3d dimes\n", dimes); 
            printf("\n%3d nickels\n", nickels); 
            printf("\n%3d pennies\n",pennies); 
     }      
     {           
        printf("\nPlease enter an amount of money: \n"); 
        scanf("%f", &total); 
               
        change(total, &quarters, &dimes, &nickels, &pennies); 
            printf("TOTAL VALUE ENTERED: $%0.2f",total); 
            printf("\n%3d quarters\n",quarters); 
            printf("\n%3d dimes\n",dimes); 
            printf("\n%3d nickels\n",nickels); 
            printf("\n%3d pennies\n",pennies); 
     }    
    fflush(stdin);   /* clear input area so you can pause */
        getchar();   /* force the computer to pause until you press a key on the keyboard */
    
    }
    
          int change(float total, int *quarters, int *dimes, int *nickels, int *pennies)
    {   
          if( total >= 0.25 )
          *quarters = (total/0.25);
          if( total >= 0.10 )
          *dimes = (total - (*quarters * 0.25))/0.10;
          if( total >= 0.05 )
          *nickels = (total - (*quarters * 0.25) - (*dimes * 0.10))/0.05;
          if( total >= 0.01)
          *pennies = (total - (*quarters * 0.25) - (*dimes * 0.10) - (*nickels * 0.05))/0.01 + .005;
    		
    		
    		
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Feb 2011
    Posts
    7
    Example of bad entry is if I put in $0.01 it tells me that I have 1 quarter, 0 dimes, 1 nickel and 2 pennies, which is completely wrong. Anything over $0.25 is a correct count.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Your program would be much easier to read if you formatted it properly and say, moved all those repeated printf calls into a function, e.g.,
    Code:
    #include <stdio.h>
    
    int change(float total, int *quarters, int *dimes, int *nickels, int *pennies);
    void print(float total, int quarters, int dimes, int nickels, int pennies);
    
    int main(void)
    {
        int quarters, dimes, nickels, pennies;
        float total;
    
        total = 1.88;
        change(total, &quarters, &dimes, &nickels, &pennies);
        print(total, quarters, dimes, nickels, pennies);
    
        total = 0.32;
        change(total, &quarters, &dimes, &nickels, &pennies);
        print(total, quarters, dimes, nickels, pennies);
    
        printf("\nPlease enter an amount of money: \n");
        scanf("%f", &total);
    
        change(total, &quarters, &dimes, &nickels, &pennies);
        print(total, quarters, dimes, nickels, pennies);
    
        return 0;
    }
    
    int change(float total, int *quarters, int *dimes, int *nickels, int *pennies)
    {
        if( total >= 0.25 )
            *quarters = (total / 0.25);
        if( total >= 0.10 )
            *dimes = (total - (*quarters * 0.25)) / 0.10;
        if( total >= 0.05 )
            *nickels = (total - (*quarters * 0.25) - (*dimes * 0.10)) / 0.05;
        if( total >= 0.01 )
            *pennies = (total - (*quarters * 0.25) - (*dimes * 0.10) - (*nickels * 0.05)) / 0.01 + .005;
    
        return 0;
    }
    
    void print(float total, int quarters, int dimes, int nickels, int pennies)
    {
        printf("\nTOTAL VALUE ENTERED: $%.2f", total);
        printf("\n%3d quarters\n", quarters);
        printf("\n%3d dimes\n", dimes);
        printf("\n%3d nickels\n", nickels);
        printf("\n%3d pennies\n", pennies);
    }
    As for your problem: you failed to set all those variables to 0. In fact, you should do so at the start of the change function, e.g., assign *quarters = 0;
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    7
    The purpose for writing it out is this is a basic class, and as dictated by the professor we are to write everything out at this point in time.

    As far as what you said, it didn't make a difference. Like I said, I am not looking for rewrite, but possible reasons it doesn't work under 0.25.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
        int quarters = 0, dimes = 0, nickels = 0, pennies = 0;
        float total = 0.0;
    As laserlight is telling you... you need to initialize variables otherwise you do not know what random values they may hold.

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    Quote Originally Posted by Scrogdor
    As far as what you said, it didn't make a difference.
    I don't believe you. Do it as laserlight said, at the beginning the of the change() function.

    Quote Originally Posted by CommonTater View Post
    Code:
        int quarters = 0, dimes = 0, nickels = 0, pennies = 0;
        float total = 0.0;
    As laserlight is telling you... you need to initialize variables otherwise you do not know what random values they may hold.
    That won't work. It needs to be done at either the beginning of the change() function or before every call to the change() function. Otherwise, you'll call change() for the 0.32 value and then when you call it for the user-supplied value the variables will still have the left over values from the last call.
    Last edited by itsme86; 02-22-2011 at 02:24 PM.
    If you understand what you're doing, you're not learning anything.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,512
    By the way, you said main would return an int, so please do so: Cprogramming.com FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]).

    Also, fflush(stdin) is wrong. Here are some alternatives if you need to clear the input buffer: Cprogramming.com FAQ > Flush the input buffer.

  8. #8
    Registered User
    Join Date
    Feb 2011
    Posts
    7
    Quote Originally Posted by itsme86 View Post
    I don't believe you. Do it as laserlight said, at the beginning the of the change() function.



    That won't work. It needs to be done at either the beginning of the change() function or before every call to the change() function. Otherwise, you'll call change() for the 0.32 value and then when you call it for the user-supplied value the variables will still have the left over values from the last call.
    If that is the case, and the .32 count affects the user input count, why does everything over 0.25 work as a correct count, but nothing under 0.25 does?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Quote Originally Posted by Scrogdor
    If that is the case, and the .32 count affects the user input count, why does everything over 0.25 work as a correct count, but nothing under 0.25 does?
    Examine:
    Code:
        if( total >= 0.25 )
            *quarters = (total / 0.25);
    Suppose total is less than 0.25. Then total >= 0.25 evaluates to false, hence *quarters is left with its previous value.

    In fact, try this: enter 0 as the total. Notice that the results are exactly the same as for $0.32.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Feb 2011
    Posts
    7
    Quote Originally Posted by laserlight View Post
    Examine:
    Code:
        if( total >= 0.25 )
            *quarters = (total / 0.25);
    Suppose total is less than 0.25. Then total >= 0.25 evaluates to false, hence *quarters is left with its previous value.

    In fact, try this: enter 0 as the total. Notice that the results are exactly the same as for $0.32.
    Now that makes perfect sense. This is my first class of C Programming and knew nothing of it before. So I am learning. Thank you for taking the time to explain, I really appreciate it.

  11. #11
    Registered User
    Join Date
    Feb 2011
    Posts
    7
    Thank you for everyones help. I have added the code that was suggested as far as setting everything to 0 before each call. I didn't realize that the third call would build off the second one if no values were entered in to make them change. You guys rock!

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by laserlight View Post
    Examine:
    Code:
        if( total >= 0.25 )
            *quarters = (total / 0.25);
    Ok... get out your calculators... lets say the total is 100 ... what is 100 / 0.25?

  13. #13
    Registered User
    Join Date
    Feb 2011
    Posts
    7
    Quote Originally Posted by anduril462 View Post
    By the way, you said main would return an int, so please do so: Cprogramming.com FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]).

    Also, fflush(stdin) is wrong. Here are some alternatives if you need to clear the input buffer: Cprogramming.com FAQ > Flush the input buffer.
    Nothing wrong about it. It does what it is supposed to do so the teacher that is grading our assignment can see the work in action. We do as he directs, not as we want.

  14. #14
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    Quote Originally Posted by Scrogdor View Post
    Nothing wrong about it.
    Wow, when are you going to learn to listen? It already bit you once in this thread and you still continue to argue with the people you're asking for help from. Just because your teacher does it that way doesn't mean there's nothing wrong about it. I for one am done helping you.
    If you understand what you're doing, you're not learning anything.

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Scrogdor View Post
    Nothing wrong about it. It does what it is supposed to do so the teacher that is grading our assignment can see the work in action. We do as he directs, not as we want.
    Ummmm... Hate to but in here but errors such as fflush(stdin); are common mistakes that, if not corrected, get into real world code and then cause problems. Good programming practices start with "hello world" and build from there. If your instructor tells you otherwise... he's wrong.
    ARod609 likes this.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C program to Calculate Change
    By CaptMorgan in forum C Programming
    Replies: 8
    Last Post: 04-07-2010, 12:31 AM
  2. making a program leave a msg for background program when it closes
    By superflygizmo in forum Windows Programming
    Replies: 2
    Last Post: 02-06-2006, 06:44 PM
  3. Replies: 1
    Last Post: 01-24-2005, 01:07 PM
  4. Change of base program
    By cdonlan in forum C Programming
    Replies: 2
    Last Post: 01-17-2005, 03:51 PM
  5. counting program worked, but test file CRASHES IT.. WHY?
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 05-19-2002, 02:29 AM

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