Accumulate and Find_if (LAMBDA)

I am doing a program where the user chooses to create a list either with ints or doubles. The classes need therefor to be templated. In these classes i also have function to calculate the avarage, the sum and also find the first occuring value between 1500 and 1900.

But when i try to calculate the sum i only get 0, when i try to calculate avarage it crashes, when i try to find first between 1500 and 1900 it only returns a weird number

What is happening? Trying to understand Lambda

ListManipulator.hpp
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#pragma once

#include <list>
#include <random>
#include <ctime>
#include <numeric>
#include <functional>
#include <iterator>



template<typename T>
class ListManipulator {
private:
	std::list<T> *theList;

public:
	ListManipulator() { theList = new std::list<T>[20]; }
	ListManipulator(std::list<T> *aList);
	~ListManipulator() {}

	void fillList();
	T sumList() const;
	T listAverage() const;
	bool findFirst1500_1900(T &num) const;
	void divideByTwo();
	void swapPlaces();
	void findMinMax(T &min, T &max) const;
	void sortList();
	void clearList();
	std::list<T> getList() const;
	void saveToFile() const;
	void loadFromFile();
	T random1();

};



template<typename T> ListManipulator<T>::ListManipulator(std::list<T> *aList) {
	theList(aList);
}

template<typename T>
T ListManipulator<T>::random1()
{
	std::uniform_real_distribution<double> random(1000, 2000);
	std::default_random_engine generator(static_cast<unsigned>(std::time(0)));

	return random(generator);
}
// find the first occuring number with value between 1500-1900
template<typename T> 
bool ListManipulator<T> ::findFirst1500_1900(T &num) const
{
	auto it = find_if(theList->begin(), theList->end(), [num](T a) {
		return (a >= 1500 && a <= 1900); });
	return true;

		return false;
}

// fill the list with 20 random numbers
template<typename T>
void ListManipulator<T>::fillList()
{
	std::uniform_real_distribution<double> random(1000, 2000);
	std::default_random_engine generator(static_cast<unsigned>(std::time(0)));
	
	auto myGenerator = std::bind(random, generator);
	//std::generate(theList->begin(), theList->end(), myGenerator);
	
	std::generate(theList->begin(), theList->end(), myGenerator);


	/*default_random_engine randomGenerator(random_device{}());
	my_distribution<T> distribution(1000, 2000);
	auto myGenerator = bind(distribution, randomGenerator);
	generate(theList.begin(), theList.end(), myGenerator);*/
}

template<typename T>
T ListManipulator<T>::sumList() const
{
	auto first = theList->begin();
	auto last = theList->end();
T total=std::accumulate(first, last, T(0), [](T all, T each) { return all + each; });
return total;
}

template<typename T>
T ListManipulator<T>::listAverage() const
{
	auto first = theList->begin();
	auto last = theList->end();
	return std::accumulate(first, last, T(0)) / theList->size();
}

UserInterface.hpp
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#pragma once

// include the appropriate header and define the clearScr and Sleep functions depending on platform used
#if defined __WIN32__ || defined _WIN32
#include <windows.h>
#define clearScr() system("cls")
#else 
#include <unistd.h>
#define Sleep(x) usleep((x)*1000)
#define clearScr() printf("\033[H\033[J")
#endif

#include "ListManipulator.hpp"
#include <memory>
#include <list>
#include <iostream>
#include <string>
#include <iomanip>


template<typename T>
class UserInterface
{
private:
ListManipulator<T> *myList;
T minValue;
T maxValue;
bool done;
	
public:

	UserInterface() : minValue(0), maxValue(0), done(true) { myList = new ListManipulator<T>; }
	~UserInterface(){}


	bool chooseList();
	void runPro();
	void getMinMax();
	void fillList();
	void menu();
	void choice();
	void first1500_1900();

	
};

void goOn(const std::string &message);
void clearScreen();





using namespace std;

void goOn(const std::string &message)
{
	std::cout << message << std::endl;
	std::cin.get();
}

void clearScreen() {

	clearScr();
}


template<typename T>
bool UserInterface<T>::chooseList() {
	std::list<int> *intList;
	std::list<double> *doubleList;
	int choice;
	std::cout << "Choose type of list." << std::endl
		<< "1. Int." << std::endl
		<< "2. Double." << std::endl;

	std::cin >> choice;
	std::cin.get();

	switch (choice) {

	case 1: return true;
		break;
	case 2: return false;
		break;
	}

}

template<typename T>
void UserInterface<T>::menu() {

	std::cout << "##### MENU #####" << std::endl
		<< "1. Fill list" << std::endl
		<< "2. Summerize list" << std::endl
		<< "3. Average value" << std::endl
		<< "4. Find first number between 1500-1900" << std::endl
		<< "5. Divied by two" << std::endl
		<< "6. Swap places" << std::endl
		<< "7. Find Min and Max" << std::endl
		<< "8. Sort" << std::endl
		<< "9. Clear list" << std::endl
		<< "10. Write to file" << std::endl
		<< "11. Load from file" << std::endl
		<< "12. Print list" << std::endl
		<< "13. Quit" << std::endl;
}

template<typename T>
void UserInterface<T>::choice()
{
	int choice;
	std::cout << "My choice: " << flush;
	std::cin >> choice;
	std::cin.get();

	switch (choice) {

	case 1: fillList();
		break;
	case 2: std::cout << "The sum is: " << myList->sumList() << std::endl;
		goOn("Press enter for main menu...");
		break;
	case 3: std::cout << "The avarage is: " << myList->listAverage() << std::endl;
		goOn("Press enter for main menu...");
		break;
	case 4: first1500_1900();
	case 13: done = false;
		break;
	}
}

void chooseType()
{


}

template<typename T> void UserInterface<T>::runPro()
{
	while (done) {
		clearScreen();
		menu();
		choice();
	}

}

template<typename T> void UserInterface<T>::first1500_1900()
{
	T e;
		if (myList->findFirst1500_1900(e))
			std::cout << "First between 1500-1900 is: " << e << std::endl;
		else
			std::cout << "No number between 1500-1900" << std::endl;
		goOn("Press enter for main menu...");
}

template<typename T> void UserInterface<T>::getMinMax()
{
	myList.FindMinMax(minValue, maxValue);
	std::cout << "Min value is: " << minValue << std::endl
		<< "Max value is: " << maxValue << std::endl;
	goOn("Press enter for main menu...");
}

template<typename T> void UserInterface<T>::fillList()
{
	clearScreen();
	myList->fillList();
	std::cout << "The list has been filled with 20 numbers." << std::endl;
	goOn("Press enter for main menu...");
}

main.cpp
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
#include "UserInterface.hpp"
#include <iostream>


int main() {
	bool looping = true;

	while (looping) {
		int choice;
		std::cout << "Choose type of list." << std::endl
			<< "1. Int." << std::endl
			<< "2. Double." << std::endl;

		std::cin >> choice;
		std::cin.get();
		if (choice == 1) {
			UserInterface<int> finalUI;
			finalUI.runPro();
		
		}
		else if (choice == 2) {
			UserInterface<float> finalUI;
			finalUI.runPro();
		
		}
		else
			std::cout << "Please choose an option between 1 and 2" << std::endl;
	}


}
You seem to be expecting num to magically get a value in findFirst1500_1900. Try something like this (untested and I've only taken a very quick look at your code).
1
2
3
4
5
6
7
8
9
10
template<typename T> 
bool ListManipulator<T>::findFirst1500_1900(T &num) const {
	auto it = find_if(theList->begin(), theList->end(), [](T a) {
		return (a >= 1500 && a <= 1900); });
	if (it != theList->end()) {
	    num = *it;
	    return true;
	}
	return false;
}

closed account (SECMoG1T)
hello i haven't read through your code but from a glance one bug stands out.

1
2
3
4
5
6
std::list<T> *theList;//from the way you call fillist and sumlist i assume you want theList
     ///to point to a list of T elements

theList = new std::list<T>[20];///instead you creating an array of 20 such lists

theList = new std::list<T>(20);//maybe this is what you intended, 20 elements of type T 
You fillList function does nothing. Look at this line:
 
	std::generate(theList->begin(), theList->end(), myGenerator);

You are asking it to go from begin() to end(), which is...what? The list is empty. So it does nothing.
Topic archived. No new replies allowed.