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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <stdio.h>


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