How to update the integer in other function

Every time I try to run this code, all the output become 0. Can anyone help me out? So, here's my code:

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
51
52
53
54
55
56
57
58
59
60
61
  #include <iostream>
using namespace std;
void countFreq(int);
int sumPos(int);
int sumNeg(int);

int main ()
{
	int a, totp, totn;
	
	cout << "Enter 10 integers: " << endl;
	
	for (int i=0;i<10;i++)	
	{
		cin >> a;
	}
	
	totp = sumPos(a);
	totn = sumNeg(a);
	
	countFreq(a) ; 
	cout <<endl << "The sum of positive number is: " << totp<< endl;
	cout << "The sum of positive number is: " << totn;

	return 0;
}

void countFreq(int c)
{
	
	int pn=0, nn=0, z=0;
	
	if (c > 0)
		pn += 1;
		
	else if (c<0)
		nn += 1;
		
	else
		z += 1;
	
	cout << "There are " << pn << " positive numbers, " << nn << " negative numbers and " << z << " zeroes.";
}

int sumPos(int a)
{
	int tpn;
	
	tpn += a;
	
	return tpn;	
}

int sumNeg(int b)
{
	int tnn;
	
	tnn += b;
	
	return tnn;
}
Hi jujull ,

Your code have a very basic issue. The loop which is taking input in "a" is overwriting the value of "a" after every input. So at the end of this loop you have only one value of "a".
1
2
3
4
for (int i=0;i<10;i++)	
	{
		cin >> a;
	}


what you should do is create array instead of single variable
Try this:
1
2
3
4
5
6
7
8
9
10
11


int totp=0, totn=0;
	for (int i=0;i<10;i++)	
	{
		cin >> a;
		totn = totn+a;
                if(a > 0) 
                    totp = totp+a;
		
	}


Assuming totn is total sum of all numbers and totp is total sum of only positive numbers. ;)
He don't have to create an Array or something he only should make the operations with a in his for-Loop like this:

1
2
3
4
5
6
for (int i=0;i<10;i++)	
	{
		cin >> a;
                totp = sumPos(a);
	        totn = sumNeg(a);
	}



This assignment was before we learn array, so we shouldn't using any array. Btw, thanks guys for the help. I really appreciate a lot!
Topic archived. No new replies allowed.