### sum problem with functions in C I'm a beginner with programming in C and I have a problem to solve at which point I get stuck.

So my program reads numbers until the value 0 then it calculates the sum of numbers. Then I have to write a function which displays the sum. Also, my program read a number "y" from keyboard and I need to find the result of the sum/y.For example if the result of the sum is 10 and I enter y=3 the function result should return the result of 10/3.

My program need to be built with functions. I received the functions name with parameters and I built them like this:

Below are the functions I coded.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950`` ``````#include // Shows a message with what the program is doing. void ShowIntroduction(void) { printf("My program finds a sum etc"); } // find the sum of the numbers enter until 0 value int sum(void) { int s=0,n; do { scanf("%d",&n); if (n > 0) s=s+n; } while(n != 0); return s; } // show the result void sumResult(int a) { printf("The sum is %d", a); } // find the result of sum/y double result(int s,int y) { double res; res=s/(double)(y); return res; } int main() { int y; scanf("%d",&y); ShowIntroduction(); sumResult(sum()); result(sum(),y); return 0; }``````

The problem is that my program finds the sum but when it should display the result of the sum / y it asks me to give the values again for the sum until 0 value then the program closes.I think I apelate the result function in a wrong way.How to correct this? The problem is that my program finds the sum but when it should display the result of the sum / y it asks me to give the values again
That's because sum() is reading from stdin.

You could call sum() once and store the result in a local variable, then pass that value to sumResult() and then result(). Thank you very much!
I solved the problem.
Topic archived. No new replies allowed.