# need help calling a function, please.

This is a discussion on need help calling a function, please. within the C Programming forums, part of the General Programming Boards category; Originally Posted by rosemary Ok. Not rounding, but just not showing the cents. Is this because of using integers? Yes, ...

1. Originally Posted by rosemary
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. 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. Originally Posted by rosemary
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. Originally Posted by AndiPersti
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. 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. 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. 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. Originally Posted by AndiPersti
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. 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.

10. 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.  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.

11. 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. 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. Originally Posted by whiteflags
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. Change this.

Originally Posted by rosemary
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.

15. 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.

Page 3 of 4 First 1234 Last
Popular pages Recent additions