Thread: need help calling a function, please.

  1. #16
    Registered User
    Join Date
    Jul 2012
    Location
    Australia
    Posts
    242
    Here is the output from my compiler:
    Code:
    |8|error: conflicting types for ‘remove’|
    /usr/include/stdio.h|179|note: previous declaration of ‘remove’ was here|
    ||In function ‘main’:|
    |32|warning: format ‘%d’ expects argument of type ‘int’, but argument 6 has type ‘double’ [-Wformat]|
    ||=== Build finished: 2 errors, 1 warnings ===|
    There is something wrong with the declaration of the remove function.

    I also noticed that when you call the functions(including remove), that you pass the float(x) as the last argument, yet in the function headings, you declare float as the first parameter. The order in which the arguments are passed to the function must be in the order that the parameters are declared in the prototype.

    So if you have:
    Code:
    int remove (float x, int p, int n, int d, int q)
    Then calling the function should look like:
    Code:
    remove(x, pennies, nickels, dimes, quarters); //  your code has x as the last argument, instead of first
    IDE: Code::Blocks | Compiler Suite for Windows: TDM-GCC (MingW, gdb)

  2. #17
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    remove is a standard function name that is what is wrong with it. Mostly.

  3. #18
    Registered User
    Join Date
    Jul 2012
    Location
    Australia
    Posts
    242
    Quote Originally Posted by whiteflags View Post
    remove is a standard function name that is what is wrong with it. Mostly.


    rosemary, what whiteflags means is that "remove" is a keyword(actually a function name) already used by the C language. You can't use the same name, so you will have to rename the "remove" function to something else ie. remove1.
    IDE: Code::Blocks | Compiler Suite for Windows: TDM-GCC (MingW, gdb)

  4. #19
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    Why doesn't the program use the insert function? it gives me a total of $0, after I enter my coins. Thanks.
    Code:
    #include <stdio.h>
    
    
    int insert (float x, int p, int n, int d, int q)//
    {
    	return (x + p + (5*n) + (10*d) + (25*q))/100;
    }
    int remove1 (float x, int p, int n, int d, int q)//
    {
    	return (x - p - (5*n) - (10*d) - (25*q))/100;
    }
    
    
    
    
    int main(void)
      {   
      int pennies, nickels, dimes, quarters, p,n,d,q;
      pennies=0;
      nickels=0;
      dimes=0;
      quarters=0;
      float x = 0;
    
    
    printf( " %d pennies + %d nickels + %d dimes + %d quarters = $%d \n", pennies, nickels, dimes, quarters, x);
    printf("insert");
    scanf("%d%d%d%d", &p, &n, &d, &q);
    pennies += p;
    nickels += n;
    dimes += d;
    quarters += q;
    insert(x, pennies, nickels, dimes, quarters);
    {
    printf( " %d pennies + %d nickels + %d dimes + %d quarters = $%d \n", pennies, nickels, dimes, quarters, x);
    }
    
    
    printf("remove");
    scanf("%d%d%d%d", &p, &n, &d, &q);
    pennies += p;
    nickels += n;
    dimes += d;
    quarters += q;
    remove1(x, pennies, nickels, dimes, quarters);
    {
    printf( " %d pennies + %d nickels + %d dimes + %d quarters = $%d \n", pennies, nickels, dimes, quarters, x);
    }
    return 0;
    }

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    One of the things that you need to do is to indent (and format) your code properly, e.g.,
    Code:
    #include <stdio.h>
    
    int insert (float x, int p, int n, int d, int q)
    {
        return (x + p + (5 * n) + (10 * d) + (25 * q)) / 100;
    }
    
    int remove1 (float x, int p, int n, int d, int q)
    {
        return (x - p - (5 * n) - (10 * d) - (25 * q)) / 100;
    }
    
    int main(void)
    {
        int pennies, nickels, dimes, quarters, p, n, d, q;
        pennies = 0;
        nickels = 0;
        dimes = 0;
        quarters = 0;
        float x = 0;
    
        printf(" %d pennies + %d nickels + %d dimes + %d quarters = $%d \n", pennies, nickels, dimes, quarters, x);
        printf("insert");
        scanf("%d%d%d%d", &p, &n, &d, &q);
        pennies += p;
        nickels += n;
        dimes += d;
        quarters += q;
        insert(x, pennies, nickels, dimes, quarters);
        {
            printf( " %d pennies + %d nickels + %d dimes + %d quarters = $%d \n", pennies, nickels, dimes, quarters, x);
        }
    
        printf("remove");
        scanf("%d%d%d%d", &p, &n, &d, &q);
        pennies += p;
        nickels += n;
        dimes += d;
        quarters += q;
        remove1(x, pennies, nickels, dimes, quarters);
        {
            printf(" %d pennies + %d nickels + %d dimes + %d quarters = $%d \n", pennies, nickels, dimes, quarters, x);
        }
    
        return 0;
    }
    Now, I observe that you call the insert and remove1 functions, you don't make use of the return value. This accounts for why the program does not appear to "use the insert function". I notice that you also have code in unnecessary braces.

    Besides this, you should use descriptive names. pennies and nickles: yes. p and n: no.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #21
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    Quote Originally Posted by laserlight View Post
    you don't make use of the return value
    I thought I was already doing that in the first few lines? Thanks.

    Code:
    #include <stdio.h>
    
    
    int insert (float x, int pennies, int nickels, int dimes, int quarters)//
    {
    	return (x + pennies + (5*nickels) + (10*dimes) + (25*quarters))/100;
    }
    int remove1 (float x, int pennies, int nickels, int dimes, int quarters)//
    {
    	return (x - pennies - (5*nickels) - (10*dimes) - (25*quarters))/100;
    }
    
    
    
    
    int main (void)
      {   
      int pennies, nickels, dimes, quarters, p,n,d,q;
      pennies = 0;
      nickels = 0;
      dimes = 0;
      quarters = 0;
      float x = 0;
    	
      printf ( " %d pennies + %d nickels + %d dimes + %d quarters = $%d \n", pennies, nickels, dimes, quarters, x);
      printf (" insert ");
      scanf (" %d%d%d%d ", &p, &n, &d, &q);
      	pennies += p;
      	nickels += n;
    	dimes += d;
    	quarters += q;
      insert (x, pennies, nickels, dimes, quarters);
      printf ( " %d pennies + %d nickels + %d dimes + %d quarters = $%d \n", pennies, nickels, dimes, quarters, x);
    
    
      printf (" remove ");
      scanf (" %d%d%d%d ", &p, &n, &d, &q);
      	pennies += p;
      	nickels += n;
        dimes += d;
        quarters += q;
      remove1 (x, pennies, nickels, dimes, quarters);
      printf ( " %d pennies + %d nickels + %d dimes + %d quarters = $%d \n", pennies, nickels, dimes, quarters, x);
    
    
    return 0;
    }

  7. #22
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by rosemary
    I thought I was already doing that in the first few lines?
    This is a function definition:
    Code:
    int insert(float x, int pennies, int nickels, int dimes, int quarters)//
    {
        return (x + pennies + (5*nickels) + (10*dimes) + (25*quarters))/100;
    }
    This is a function call:
    Code:
    insert(x, pennies, nickels, dimes, quarters);
    In your function definition, you declared the function as returning an int, then you return an int. But when you called the function you did not make use of the value returned. For example, we might write:
    Code:
    x = insert(pennies, nickels, dimes, quarters);
    Notice that I took the liberty of removing x from the argument list as I suspect that it does not belong there. In fact, I suspect that balance would be a better name than x.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #23
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    Thanks. But i'm still not accessing the function.
    Code:
    #include <stdio.h>
    
    
    int insert (int pennies, int nickels, int dimes, int quarters)//
    {
    	return (pennies + (5*nickels) + (10*dimes) + (25*quarters))/100;
    }
    int remove1 (int pennies, int nickels, int dimes, int quarters)//
    {
    	return (- pennies - (5*nickels) - (10*dimes) - (25*quarters))/100;
    }
    
    
    
    
    int main (void)
      {   
      int pennies, nickels, dimes, quarters, p,n,d,q;
      pennies = 0;
      nickels = 0;
      dimes = 0;
      quarters = 0;
      float x = 0;
    	
      printf ( " %d pennies + %d nickels + %d dimes + %d quarters = $%d \n", pennies, nickels, dimes, quarters, x);
      printf (" insert ");
      scanf (" %d%d%d%d ", &p, &n, &d, &q);
      	pennies += p;
      	nickels += n;
    	dimes += d;
    	quarters += q;
      x = insert (pennies, nickels, dimes, quarters);
      printf ( " %d pennies + %d nickels + %d dimes + %d quarters = $%d \n", pennies, nickels, dimes, quarters, x);
    
    
      printf (" remove ");
      scanf (" %d%d%d%d ", &p, &n, &d, &q);
      	pennies += p;
      	nickels += n;
        dimes += d;
        quarters += q;
      x = remove1 (pennies, nickels, dimes, quarters);
      printf ( " %d pennies + %d nickels + %d dimes + %d quarters = $%d \n", pennies, nickels, dimes, quarters, x);
    
    
    return 0;
    }

  9. #24
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What do you mean?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #25
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    My guess is that scanf() is waiting for input. That is its purpose, after all.

    If you type input as "1234", the first scanf call will give p the value of 1234, then wait for further input. There need to be spaces between numeric values that you type in, even if the format string doesn't include spaces.

    Including a space at the end of the format string also causes scanf() to keep waiting until whitespace is entered.

    It doesn't help that the printf() calls use a %d format specifier for x (which is of type float). Compilers often warn about that. Whether compilers warn about it or not, though, the resultant behaviour is undefined.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  11. #26
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    yes, the white space. thanks.

  12. #27
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    ok. my insert function is pretty close except for it is rounding. how do i change that? my remove function is not working correctly. thanks.
    Code:
    #include <stdio.h>
    
    
    int insert (int pennies, int nickels, int dimes, int quarters)//
    {
    	return (pennies + (5*nickels) + (10*dimes) + (25*quarters))/100;
    }
    int remove1 (float x, int pennies, int nickels, int dimes, int quarters)//
    {
    	return (x - (.01*pennies + (.05*nickels) + (.10*dimes) + (.25*quarters)));
    }
    
    
    
    
    int main (void)
      {   
      int pennies, nickels, dimes, quarters, p,n,d,q;
      pennies = 0;
      nickels = 0;
      dimes = 0;
      quarters = 0;
      float x = 0;
    	
      printf ( " %d pennies + %d nickels + %d dimes + %d quarters = $%.2f \n", pennies, nickels, dimes, quarters, x);
      printf (" insert ");
      scanf ("%d%d%d%d", &p, &n, &d, &q);
      	pennies += p;
      	nickels += n;
    	dimes += d;
    	quarters += q;
      x = insert (pennies, nickels, dimes, quarters);
      printf ( " %d pennies + %d nickels + %d dimes + %d quarters = $%.2f \n", pennies, nickels, dimes, quarters, x);
    
    
      printf (" remove ");
      scanf ("%d%d%d%d", &p, &n, &d, &q);
      	pennies -= p;
      	nickels -= n;
        dimes -= d;
        quarters -= q;
      x = remove1 (x, pennies, nickels, dimes, quarters);
      printf ( " %d pennies + %d nickels + %d dimes + %d quarters = $%.2f \n", pennies, nickels, dimes, quarters, x);
    
    
    return 0;
    }

  13. #28
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You are too vague. What do you mean by "pretty close except for it is rounding"? What do you mean by "not working correctly"? What is your test input, expected output and actual output?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #29
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    If I insert $1.41 in change, my total displays $1.00.
    If I insert $2.41 in change, my total displays $2.00.

  15. #30
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    Ok. Not rounding, but just not showing the cents. Is this because of using integers?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 09-26-2011, 04:44 AM
  2. Calling a function inside a function
    By kizyle502 in forum C Programming
    Replies: 1
    Last Post: 09-17-2009, 11:29 AM
  3. Replies: 15
    Last Post: 06-09-2009, 02:19 AM
  4. Calling Cdocument function in Cview function
    By RancidWannaRiot in forum Windows Programming
    Replies: 5
    Last Post: 09-22-2005, 12:09 PM
  5. Question on function syntax and calling function
    By cbrman in forum C Programming
    Replies: 10
    Last Post: 10-05-2003, 05:32 PM