Coin Counting Help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • karafire2003
    New Member
    • Apr 2007
    • 10

    Coin Counting Help

    Hey there i was wondering if someone can help me with my program. I thought i had it down...but i'm having some problems. Here's the assignment.

    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.

    Call the function change() from main() three times and print out the contents of the variables quarters, dimes, nickels, and pennies after each function return.

    First Call--pass in the total value $1.88 and on return print the contents of the variables.

    Second Call--pass in the total value .32 and on return print the contents of the variables.

    Third Call--ask for a total value input from the keyboard and on return print the contents of the variables.

    Output should look like:

    TOTAL VALUE ENTERED: 1.88
    7 quarters
    1 dime
    0 nickels
    3 pennies


    and not:

    TOTAL VALUE ENTERED: 1.88
    7 quarters
    18 dimes
    37 nickels
    188 pennies

    Here's my code:
    Code:
    #include <stdio.h>
    int main()
    {
        void change (float);
        float value;
        int count, i;
        printf("Enter the value you wish to count: ");
        scanf("%d", count);
        for (i = 0, i <= count, i++)
        {
            printf("Please enter the amount: ");
            scanf("%f", value);
        }
        return 0;
    }
    
        void change (float x)
        int quarters, dimes, nickles, pennies;
        float value, store;
        value = x;
        store = x;
        
        while (value >= .25)
        {
              quarters ++;
              value = value - .25;
        }
        
        while (value >= .1)
        {
              dines ++;
              value = value - .1:
        }
        
        while (value >= .05)
        {
              nickels ++;
              value = value - .05;
        }
        
        while (value >= .01)
        {
              pennies ++;
              value = value - .01;
        }
        
        printf("The total value entered was: %f ", store);
        printf("%d" quarters, quarters\n);
        printf("%d" dimes, dimes\n);
        printf("%d" nickles, nickles\n);
        printf("%d" pennies, pennies\n);
    }
    I'm getting a slew of errors once i compile. if anyone can help it would be greatly appreciated. Thanks for your time :)
    Last edited by sicarie; May 11 '07, 01:57 PM. Reason: Please use [code] and [/code] tags around your code. Thanks!
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    Are you getting error messages, or the "wrong" output you posted? If error messages, can you post them?

    Comment

    • scruggsy
      New Member
      • Mar 2007
      • 147

      #3
      Code:
      for (i = 0, i <= count, i++)
      You'll get a redeclaration error here. Replace the commas with semicolons.
      Code:
      int quarters, dimes, nickles, pennies;
      float value, store;
      value = x;
      store = x;
      
      while (value >= .25)
      {
      quarters ++;
      value = value - .25;
      }
      quarters, dimes, nickles, and pennies should be initialized to 0 before you use them.
      I assume your mismatched spelling of "dimes"/"dines" and "nickles"/"nickels" is just the result of typing the code up for the forums. If not, that's another source of errors.

      Comment

      • karafire2003
        New Member
        • Apr 2007
        • 10

        #4
        Yes you are RIGHT, i did have some spelling differences between quarters, nickels, dimes, and pennies. hehehehe
        here are some of the errors that i got after i corrected spelling and changed the commas to semicolons:
        C:\Documents and Settings\T\Desk top\Work\Classe s\assignments\W A-3\Coins.c In function `change':
        20 C:\Documents and Settings\T\Desk top\Work\Classe s\assignments\W A-3\Coins.c syntax error before "value"
        48 C:\Documents and Settings\T\Desk top\Work\Classe s\assignments\W A-3\Coins.c stray '\' in program
        49 C:\Documents and Settings\T\Desk top\Work\Classe s\assignments\W A-3\Coins.c stray '\' in program
        50 C:\Documents and Settings\T\Desk top\Work\Classe s\assignments\W A-3\Coins.c stray '\' in program
        51 C:\Documents and Settings\T\Desk top\Work\Classe s\assignments\W A-3\Coins.c stray '\' in program

        that's it. changing the commas to semicolons cleared up a lot of the errors...thanks for the corretion :)

        Comment

        • Ganon11
          Recognized Expert Specialist
          • Oct 2006
          • 3651

          #5
          I think you are missing an opening '{' when starting your function.

          The main thing I see here is not a syntax error, but rather a discontinuity between your program and the problem specification. Your function in supposed to be accepting 5 arguments - the float variable, x, as you have, and then the addresses of quarters, dimes, nickels, and pennies. This suggests to me that these variables should be declared in your main() and then passed by reference to the change() function. Right now, you declare and use them within your function.

          If you move the variables back to the main(), you should also move your printf calls so that the output is done in main, also. change() should be assigning values to the variables only rather than displaying them.

          Comment

          • karafire2003
            New Member
            • Apr 2007
            • 10

            #6
            i don't quite follow. you have to understand i'm totally clueless about programming.

            Comment

            • Ganon11
              Recognized Expert Specialist
              • Oct 2006
              • 3651

              #7
              Originally posted by karafire2003
              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.
              This is what your assignment says. Now let's take a look at your function header:

              Code:
              void change (float x)
              Your function is only accepting one parameter - the "floating point number of total coins." But your assignment tells you to have four more variables - "the addresses of the integer variables named quarters, dimes, nickels, and pennies."

              Do you know how to pass by reference?

              Comment

              Working...