Coin Counting Program

This is a discussion on Coin Counting Program within the C Programming forums, part of the General Programming Boards category; First I want to say that this is a homework assignment I am working on. However, I do not wish ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    4

    Coin Counting Program

    First I want to say that this is a homework assignment I am working on. However, I do not wish anyone to complete the entire assignment for me. I am just confused on some error messages that I am receiving with my code. I would greatly appreciate any advice on how to simply correct the error messages. Here is the code.

    Code:
    /*
    Assignment 6
    Aaron Woods
    */
    
    #include <stdio.h>
    #define CALL1 1.88
    #define CALL2 0.32
    
    int main ()
    {
        void change(double, int, int, int, int);
        int quarters, dimes, nickels, pennies;
        double value;
    
        change();
    
        printf("TOTAL VALUE ENTERED %f", value);
        printf("%d quarters", quarters);
        printf("%d dimes", dimes);
        printf("%d nickels", nickels);
        printf("%d pennies", pennies);
    
        return 0;
    
    }
    
    void change()
    {
        static int num;
        double value;
        int quarters, dimes, nickels, pennies, count, i;
    
        if (num = 0)
        {
            value = CALL1;
    
            while (value >= .25)
    
            {
                quarters++;
                value = value - 0.25;
            }
    
            while (value >= 0.1)
    
            {
                dimes++;
                value = value - 0.1;
            }
    
            while (value >= 0.05)
    
            {
                nickels++;
                value = value - 0.05;
            }
    
            while (value >= 0.01)
    
            {
                pennies++;
                value = value - 0.01;
            }
    
            num++;
    
        }
    
        else if (num = 1)
    
        {
            value = CALL2;
    
            while (value >= .25)
    
            {
                quarters++;
                value = value - 0.25;
            }
    
            while (value >= 0.1)
    
            {
                dimes++;
                value = value - 0.1;
            }
    
            while (value >= 0.05)
    
            {
                nickels++;
                value = value - 0.05;
            }
    
            while (value >= 0.01)
    
            {
                pennies++;
                value = value - 0.01;
            }
    
            num++;
    
        }
        else if (num = 2)
    
        {
            printf("Please enter the amount of money\n");
            printf("you would like a breakdown of coins for: ");
            scanf("%f", &value);
    
            while (value >= .25)
            {
                quarters++;
                value = value - 0.25;
            }
            while (value >= 0.1)
            {
                dimes++;
                value = value - 0.1;
            }
            while (value >= 0.05)
            {
                nickels++;
                value = value - 0.05;
            }
            while (value >= 0.01)
            {
                pennies++;
                value = value - 0.01;
            }
            num++;
        }
    }
    Here are the error messages I am receiving.

    Code:
    C:\Users\Aaron Woods\Documents\School Work\C Programming\Assignment 6\Assignment 6.c||In function 'main':|
    C:\Users\Aaron Woods\Documents\School Work\C Programming\Assignment 6\Assignment 6.c|16|error: too few arguments to function 'change'|
    C:\Users\Aaron Woods\Documents\School Work\C Programming\Assignment 6\Assignment 6.c||In function 'change':|
    C:\Users\Aaron Woods\Documents\School Work\C Programming\Assignment 6\Assignment 6.c|29|error: number of arguments doesn't match prototype|
    C:\Users\Aaron Woods\Documents\School Work\C Programming\Assignment 6\Assignment 6.c|12|error: prototype declaration|
    ||=== Build finished: 3 errors, 0 warnings ===|
    I apologize for the length. Again I would greatly appreciate any assistance. I am very amateur with C. I am in my first class for it in college.

    Thanks

    Aaron

  2. #2
    THANK YOU KINDLY SIR Phenax's Avatar
    Join Date
    Mar 2011
    Posts
    74
    Your function prototype in main
    Code:
    void change(double, int, int, int, int);
    Is different than the actual declaration of your function
    Code:
    void change()
    Last edited by Phenax; 03-14-2011 at 01:23 AM.
    Quote Originally Posted by Plato
    Never discourage anyone...who continually makes progress, no matter how slow.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    4
    I'm trying to call change() three times. The first time to determine the coin breakdown of $1.88, the second $0.32, and the third as a user specified amount.

    I think what you are explaining to me might be just a bit more advanced than what I have covered so far in the course.
    Last edited by woodyhavoc; 03-14-2011 at 01:24 AM.

  4. #4
    THANK YOU KINDLY SIR Phenax's Avatar
    Join Date
    Mar 2011
    Posts
    74
    Quote Originally Posted by woodyhavoc View Post
    I'm trying to call change() three times. The first time to determine the coin breakdown of $1.88, the second $0.32, and the third as a user specified amount.

    I think what you are explaining to me might be just a bit more advanced than what I have covered so far in the course.
    Yeah, sorry, I misinterpreted your question.

    Code:
    if (num = 0)
    == tests for equality. Using = is different then ==. Your current if statement does this: Sets num to zero. This operation returns 0 and thus your if statement does not execute.

    Also, you don't set your "num" variable anywhere, you probably want to initialize it to zero.
    You can't correctly print those variables from main (quarters, dimes, etc), you're just going to get random uninitialized variables. Setting a variable equal to something in another function does not mean a variable with a same name in a different function will have the same value. This is called the scope of a variable, and most variables you declare in a function have a scope of that function.
    Last edited by Phenax; 03-14-2011 at 01:30 AM.
    Quote Originally Posted by Plato
    Never discourage anyone...who continually makes progress, no matter how slow.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    declare your coins in main: pennies, nickels, dimes, quarters, etc.

    Then send them, via their address, to your various functions, as you wish. Otherwise, if you declare them locally (not in main), then their values will be lost when your program returns from the function that the coins were declared in.

    You may call your functions as many times as you want. You may want to zero out the value of the coins, from the last call, before you call them again.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    4
    I initialized 'num' in my change() function as:

    Code:
    static int num;
    This should set num equal to zero, at least that is what I understand from what I read in my book. I moved it up to my main function though.

  7. #7
    THANK YOU KINDLY SIR Phenax's Avatar
    Join Date
    Mar 2011
    Posts
    74
    Quote Originally Posted by woodyhavoc View Post
    I initialized 'num' in my change() function as:

    Code:
    static int num;
    This should set num equal to zero, at least that is what I understand from what I read in my book. I moved it up to my main function though.
    My mistake, statics are indeed initialized to 0 by default. But I think it'd be better to explicitly initialize it to 0 to be clear about your intentions.
    Quote Originally Posted by Plato
    Never discourage anyone...who continually makes progress, no matter how slow.

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    4
    Yeah, that is a good idea. I would probably forget what I was trying to do if I don't explicitly define it.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    One more time:

    In main() you need to declare the pennies, nickels, dimes and quarters variables. If you don't, then when your program returns, the variables you have declared in change() will be gone (out of scope), and useless.

    So:
    Code:
    #include <stdio.h>
    
    /* the actual names of the variables is not needed here, just the type */
    void change(int *pennies, int *nickels, int *dimes, int *quarters); //function prototype
    
    
    int main(void) {
    
      int pennies, nickels, dimes, quarters;
    
      change(&pennies, &nickels, &dimes, &quarters);
      /* your other code here to print up the change */
    
      return 0;
    }
    void change(int *pennies, int *nickels, int *dimes, int *quarters) {
      //do not declare these variables again - they're already "here".
    
      /* do your calculations in here */
    
    }
    Usually, it's best to prototype your functions above main(), or whatever your first function is. If you change your function's parameters, then you need to change it's prototype, to match it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No return on char counting program
    By metros in forum C Programming
    Replies: 4
    Last Post: 02-09-2010, 11:07 AM
  2. Replies: 6
    Last Post: 08-11-2008, 01:50 AM
  3. Counting program
    By 182 in forum C++ Programming
    Replies: 6
    Last Post: 02-18-2006, 08:33 PM
  4. Replies: 5
    Last Post: 01-31-2006, 01:54 AM
  5. program error :counting length of string
    By bigjoke in forum C Programming
    Replies: 19
    Last Post: 12-27-2005, 03:21 PM

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