add/display functions not working

This add function is supposed to return the sum of the 5 values of the iterator. It is not doing so. I'm getting a value of 0. Also, the display function is not displaying the list of numbers. How can I solve this? Thank you.

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<iostream>
#include<string>
#include<list>
using namespace std;

list<int> aList;
list<int>::iterator it;

void getNumbers(list<int> aList);
int addNumbers(list<int> aList);
void displaySum(list<int> aList);

int main()
{

list<int> aList = {};
getNumbers(aList);
addNumbers(aList);
displaySum(aList);

}

void getNumbers(list<int> aList)
{
	int response = 0;
	cout << "Enter 5 integer values: " << endl;
	for (int i = 0; i < 5; i++)
	{
		cout << "Enter a value: ";
		cin >> response;
		aList.push_back(response);

	}
}

int addNumbers(list<int> aList)
{
	int sum = 0;
	for (it = aList.begin(); it != aList.end(); it++)
		return sum += *it;
}

void displaySum(list<int> aList)
{
	int sum = 0;
	for (it = aList.begin(); it != aList.end(); it++)
		cout << *it << ' ';

	cout << "The sum equals: " << sum << endl;
}
Last edited on
Your getNumbers() takes parameter by value. A local copy. That has no effect on the aList in main(). Pass parameter by reference.


What do you do in the main() with the int value that the addNumbers() returns? Nothing.

Your addNumbers() is equivalent to:
1
2
3
4
5
6
7
8
int addNumbers(list<int> aList)
{
  if ( aList.begin() != aList.end() ) {
    return *aList.begin();
  } else {
    // you return nothing, if list is empty. Nothing is not a int.
  }
}

You cannot return from the body of the loop, because then you skip all but first element.
Complete the loop first. Then return the result.


What is the task of the displaySum()? To display the sum? To display the elements of the list? Both?
I put the return outside the for loop in addNumbers but it still returns 0. It now displays the values of the inputted integers but still displays the sum as 0. The task of the displaySum() is to display the list of numbers just entered and the sum of their values. Currently after your help, it does display the numbers but gives me a wrong sum...(0).

Here is my altered code that doesn't work. Thanks for the help:

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
#include<iostream>
#include<string>
#include<list>
using namespace std;

list<int> aList;
list<int>::iterator it;

void getNumbers(list<int> &aList);
int addNumbers(list<int> &aList);
void displaySum(list<int> &aList);

int main()
{

list<int> aList = {};
getNumbers(aList);
addNumbers(aList);
displaySum(aList);

}

void getNumbers(list<int> &aList)
{
	int response = 0;
	cout << "Enter 5 integer values: " << endl;
	for (int i = 0; i < 5; i++)
	{
		cout << "Enter a value: ";
		cin >> response;
		aList.push_back(response);

	}
}

int addNumbers(list<int> &aList)
{
	int sum = 0;
	for (it = aList.begin(); it != aList.end(); it++)
	//not sure what to do here
	return sum += *it;
}

void displaySum(list<int> &aList)
{
	int sum = 0;
	for (it = aList.begin(); it != aList.end(); it++)
		cout << *it << ' ';

	cout << "The sum equals: " << sum << endl;
}
Last edited on
Line 6: You're declaring aList as global, but it is never used. It's declared again inside main. You should remove line 6.

Line 41: You're going to return the first time through the loop.

36
37
38
39
40
41
42
int addNumbers(list<int> &aList)
{
    int sum = 0;
    for (it = aList.begin(); it != aList.end(); it++)
        sum += *it;     // Add to the sum each time through the loop
    return sum;         // Return the total when done
}


closed account (E0p9LyTq)
You are doing nothing with the sum returned from addNumbers() (even when it is fixed to actually sum the elements of your list).

displaySum() doesn't display the sum of the list's elements, it displays the list's individual elements.
This is equivalent to your function:
1
2
3
4
5
6
7
8
9
10
void displaySum( list<int> &aList )
{
	for (it = aList.begin(); it != aList.end(); it++)
	{
		cout << *it << ' ';
	}

	int sum = 0;
	cout << "The sum equals: " << sum << endl;
}

The loop does not do anything with the variable sum.
Therefore, the variable declaration can be moved after the loop.
Now it is more clear why you see the 0; you explicitly print a 0.

You have a function that returns a value. Not just any value, but the value that you want. Why don't you call that function and then use the value that you get from the function?
I solved it. Thank you.

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
#include<iostream>
#include<string>
#include<list>
using namespace std;

list<int> aList;
list<int>::iterator it;

void getNumbers(list<int> &aList);
int addNumbers(list<int> &aList, int sum);
void displaySum(list<int> &aList);

int main()
{

list<int> aList = {};
getNumbers(aList);
displaySum(aList);

}

void getNumbers(list<int> &aList)
{
	int response = 0;
	cout << "Enter 5 integer values: " << endl;
	for (int i = 0; i < 5; i++)
	{
		cout << "Enter a value: ";
		cin >> response;
		aList.push_back(response);

	}
	cout << endl;
}

int addNumbers(list<int> &aList, int sum)
{
	for (it = aList.begin(); it != aList.end(); it++) {
		sum += *it;
	}
	return sum;
	
}

void displaySum(list<int> &aList)
{
	int sum = 0;
	cout << "Here is the list: ";
	for (it = aList.begin(); it != aList.end(); it++)
		cout << *it << ' ';

	cout << "\n\nThe sum equals: " << addNumbers(aList, sum) << endl;
}
Last edited on
Topic archived. No new replies allowed.