Thread: need help calling a function, please.

  1. #31
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by rosemary View Post
    Ok. Not rounding, but just not showing the cents. Is this because of using integers?
    Yes, your insert function uses only integers. The pennies, nickles, etc are integers, as are the 5, 10, 25. Also, the 100 is an integer. So when you do all your multiplication and addition, you get an integer, and you divide that by another integer (100). This results in "integer division", which discards any fractional part, leaving you with a whole number. Also, you currently return an integer which would also truncate the fractional part. Try the following:
    Code:
    float insert (int pennies, int nickels, int dimes, int quarters)//
    {
        return (pennies + (5*nickels) + (10*dimes) + (25*quarters))/100.0;
    }
    Note the two changes in green, that is all you need to do.

  2. #32
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    Ok. I got it to work. But my remove and insert functions are the same. That can't be correct.

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

  3. #33
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by rosemary View Post
    Ok. I got it to work. But my remove and insert functions are the same.
    Because you do all your calculations in main(), e.g. the line
    Code:
    pennies -= p;
    updates the value of "pennies". In remove() you just calculate the total.

    Code:
    float display (int pennies, int nickels, int dimes, int quarters, float total)//
    {
        return (printf ( " %d pennies + %d nickels + %d dimes + %d quarters = $%.2f \n", pennies, nickels, dimes, quarters, total));
    }
    I don't think you should return a value from display().
    It also looks like you don't understand what your return value means because you're assigning it to "total". But your return statement returns the return value of printf() which is the number of characters printed.

    Bye, Andreas

  4. #34
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    Quote Originally Posted by AndiPersti View Post
    Because you do all your calculations in main()
    So, should my insert function include-
    Code:
    	pennies += p;
      	nickels += n;
    	dimes += d;
    	quarters += q;
    or
    Code:
    (pennies + (5*nickels) + (10*dimes) + (25*quarters))/100.0

    or both? thanks.

  5. #35
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    Bear with me people. (sigh). I redid it. Please help me figure this out. What is wrong with my insert and remove functions? Thanks.

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

  6. #36
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Depends of what you want to (or have to) do.

    You should keep code which belongs together in one function. So in your case I would write a function
    Code:
    float insert(int pennies, int nickels, int dimes, int quarters, float old_total)
    which calculates the new total based on the old total and the other values and returns the new total.
    The same works with remove1(). (How about changing the name, e.g. "subtract"?)

    Bye, Andreas

  7. #37
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    int insert (int pennies, int p, int nickels, int n, int dimes, int d, int quarters, int q)//
    {
        return (pennies+=p, nickels+=n, dimes+=d, quarters+=q);
    }
    First, when you add "p" to "pennies" the new value of "pennies" will be lost as soon as you leave the function because inside the function "pennies" is a local variable.
    Second, since you can only return one value from a function you only return the result of adding "q" to "quarters" because that's the last value you calculate.

    Code:
    float dollars (int pennies, int nickels, int dimes, int quarters)//
    {
        return ((pennies + (5*nickels) + (10*dimes) + (25*quarters))/100.0);
    }
    You try to have a variable "dollars" and a function "dollars" in your program but that doesn't work. Your float "dollars" inside main() will hide the function "dollars".

    Code:
    float display (int pennies, int nickels, int dimes, int quarters, float dollars)//
    {
        return (printf ( " %d pennies + %d nickels + %d dimes + %d quarters = $%.2f \n", pennies, nickels, dimes, quarters, dollars));
    }
    You are still returning the number of characters written by printf().

    I have the impression you still don't get how functions work. Have you already read the tutorial on this site (or other tutorials/books)?

    Bye, Andreas

  8. #38
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    Quote Originally Posted by AndiPersti View Post
    Have you already read the tutorial on this site...
    Yes. Now I reread it and tried to model my program after theirs.
    Code:
    #include <stdio.h>
    
    
    int add (int pennies, int nickels, int dimes, int quarters);
    int subtract (int pennies, int nickels, int dimes, int quarters);
    int display (int pennies, int nickels, int dimes, int quarters, float x);
    
    
    int main(void)
      {   
      int pennies, nickels, dimes, quarters, p,n,d,q;
      pennies=0;
      nickels=0;
      dimes=0;
      quarters=0;
    
    
      float x = pennies + 5*nickels + 10*dimes + 25*quarters;
            x =  (float)x/100;
    
    
    printf( " %d pennies + %d nickels + %d dimes + %d quarters = $%d \n", pennies, nickels, dimes, quarters, x);
    
    
    printf("add");
    scanf("%d%d%d%d", &pennies, &nickels, &dimes, &quarters);
    printf( " %d pennies + %d nickels + %d dimes + %d quarters = $%d \n", add (pennies, nickels, dimes, quarters));
    getchar();
    
    
    printf("subtract");
    scanf("%d%d%d%d", &pennies, &nickels, &dimes, &quarters);
    printf( " %d pennies + %d nickels + %d dimes + %d quarters = $%d \n", subtract (pennies, nickels, dimes, quarters));
    getchar();
    }
    int add (int pennies, int nickels, int dimes, int quarters)
    {
    	return pennies += p;
               nickels += n;
               dimes += d;
               quarters += q;
    }
    int subtract (int pennies, int nickels, int dimes, int quarters)
    {
    	return pennies -= p;
               nickels -= n;
               dimes -= d;
               quarters -= q;
    }

  9. #39
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    OK, you are using
    Code:
      scanf("%d%d%d%d, &p, &n, &d, &q);
    and entering input "1.41" (using keyboard).

    Let's look at how scanf() will handle that. The first %d tells it to read an int. It scans the '1'. Then it scans the '.'. The '.' is not a character used to represent an integral value, so scanning stops. The value p will receive the value 1. The '.' remains ready to be read. The next part of the format string is also %d. So scanf() tries to read another int. It encounters that '.', which is not a character used to represent an integral value. This means the second integer (corresponding to the argument &n) cannot be read. Scanning stops, again with the '.' remaining ready to be read. A value for n has not been read, so scanf() returns. Only one of the values specified has been read, so scanf() returns 1.

    I am reasonably confident that is not what you are expecting that scanf() line to do with the input "1.41".

    It is not specifically because of "using integers". It is because the manner of reading (using scanf() with the %d format four times) is not relevant to the input you are supplying. scanf() will faithfully do exactly what you tell it, and report an error, and will not try to do anything else. You presumably need to work out an approach for reading two values, separate by a '.'. where the first is an integer, and the second is a two-digit integer. One way is (assuming dollars and cents are of type int).
    Code:
        scanf("%d.%2d", &dollars, &cents);
    This tells scanf() to read an integer, and a second integer composed of two digits, with a '.' between. You will then need to find some way to map the values of dollars and cents into your variables p, n, d, and q. Such mapping is not automatic (C compilers don't know anything about the relationship between dollars and cents, let alone pennies, dimes, or other things) so YOU have to write code to do that.
    Last edited by grumpy; 10-12-2012 at 02:50 PM.
    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.

  10. #40
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Code:
    int add (int pennies, int nickels, int dimes, int quarters)
    {
        return pennies += p;
               nickels += n;
               dimes += d;
               quarters += q;
    }
    int subtract (int pennies, int nickels, int dimes, int quarters)
    {
        return pennies -= p;
               nickels -= n;
               dimes -= d;
               quarters -= q;
    }
    Both of these functions will return quarters, because a function can only return one thing, and the comma operator works from left to right. [edit] Wait! WOW you aren't even using the comma operator so the situation is much worse than that. After pennies is updated, nothing is even being executed. My mistake. [/edit]

    I suppose it is a proper criticism of the tutorial that it doesn't exactly emphasize this, but I would also argue it is clear in the text of the document. A more clear example for the tutorial would be:
    Code:
     
    int mult ( int x, int y )
    {
       int ans = x * y;
       return ans;
    }
    So now the question is what value do you really want to return, if anything? You might have to collect the answer in a variable like mult does.
    Last edited by whiteflags; 10-12-2012 at 02:45 PM.

  11. #41
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    like this? but, no commas?
    Code:
    int add (int pennies, int nickels, int dimes, int quarters)
    {
    	       int ans = pennies += p, 
    		             nickels += n, 
    					 dimes += d,  
    					 quarters += q;
    		             return ans;
    }
    int subtract (int pennies, int nickels, int dimes, int quarters)
    {
    	       int ans = pennies -= p, 
    		             nickels -= n, 
    					 dimes -= d,  
    					 quarters -= q;
    		             return ans;

  12. #42
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I highly recommend, especially when it comes to math, forgetting about the comma operator.

    You probably want to update all the denominations first. Then on a separate line, decide how you want to return the information.

  13. #43
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    Quote Originally Posted by whiteflags View Post
    I highly recommend, especially when it comes to math, forgetting about the comma operator.

    You probably want to update all the denominations first. Then on a separate line, decide how you want to return the information.
    I do not understand what to do with this information, but thanks immensely for all the help from everyone. I don't want to waste anymore of your or my time.

  14. #44
    Registered User
    Join Date
    Jul 2012
    Location
    Australia
    Posts
    242
    Change this.

    Quote Originally Posted by rosemary View Post
    like this? but, no commas?
    Code:
    int add (int pennies, int nickels, int dimes, int quarters)
    {
               int ans = pennies += p, 
                         nickels += n, 
                         dimes += d,  
                         quarters += q;
                         return ans;
    }
    int subtract (int pennies, int nickels, int dimes, int quarters)
    {
               int ans = pennies -= p, 
                         nickels -= n, 
                         dimes -= d,  
                         quarters -= q;
                         return ans;
    To this.
    Code:
    int add (int pennies, int nickels, int dimes, int quarters)
    {
        return pennies += pennies;
               nickels += nickels;
               dimes += dimes;
               quarters += quarters;
    }
    int subtract (int pennies, int nickels, int dimes, int quarters)
    {
        return pennies -= pennies;
               nickels -= nickels;
               dimes -= dimes;
               quarters -= quarters;
    }
    You are calling them by full names ie. pennies on one side of the equation, and by letters ie. p on the other side. You need to make sure the names are the same everywhere.
    IDE: Code::Blocks | Compiler Suite for Windows: TDM-GCC (MingW, gdb)

  15. #45
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by cfanatic
    Change this.
    (...)
    To this.
    Code:
    int add (int pennies, int nickels, int dimes, int quarters)
    {
        return pennies += pennies;
               nickels += nickels;
               dimes += dimes;
               quarters += quarters;
    }
    int subtract (int pennies, int nickels, int dimes, int quarters)
    {
        return pennies -= pennies;
               nickels -= nickels;
               dimes -= dimes;
               quarters -= quarters;
    }
    That is still wrong though. Refer to whiteflags' post #40. Besides, if you subtract pennies from pennies, you get 0.
    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

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