Problem passing function as a parameter?

Cant seem to figure out why I can't pass my function as a parameter and I have to use this program for future programs. By the end of this week I have to not only figure out that problem but convert the struct to a class among other things. Any help would be greatly appreciated... Thank you in advance for any 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
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
 /* *** Statistical Analysis Program I ***   */


#include<iostream>
#include<fstream>
#include<string>
#include<cstring>

using namespace std;

const int arraySize = 50;

struct stat_list
{
	double list[arraySize];
	int length;
	int maxSize;
	double median;

};
// Function Prototype
stat_list file_Sort(stat_list stat_vals2);
void printArray_N_store((*file_Sort)(stat_list test_vals3));
void median_Of_Array((*file_Sort)(stat_list test_vals4));

int main()
{
	//variables
	string fileName;
	ifstream inFile;
	ofstream outFile;
	stat_list stat_vals;
	stat_vals.length = 0;
	stat_vals.maxSize = 50;

	//Infile loop
	do
	{
		cout << "Enter file name that contains the data to be sorted: \n";
		cin >> fileName;
		inFile.open(fileName.c_str());
		if (!inFile.is_open())
		{
			cout << "Error opening " << fileName << ", please try again \n";
		}
	} while (!inFile.is_open());

	//Outfile loop
	do
	{
		cout << "Enter the name of the file to store the sorted numbers: \n";
		cin >> fileName;
		outFile.open(fileName.c_str());
		if (!outFile.is_open())
		{
			cout << "Error opening " << fileName << ", please try again \n";
		}
	} while (!outFile.is_open());

	//filling array with values
	
	inFile >> stat_vals.list[stat_vals.length];
	while (inFile && stat_vals.length < stat_vals.maxSize) {
		stat_vals.length++;
		inFile >> stat_vals.list[stat_vals.length];
	}

	//Sort array
	file_Sort(stat_vals);
	printArray_N_store((&file_Sort)(stat_vals));
	median_Of_Array((&file_Sort)(stat_vals));

	outFile.close();
	inFile.close();

	return 0;
}

stat_list file_Sort(stat_list test_vals2)
{
	//Sorting
	int min;
	double temp;
	for (int i = 0; i < test_vals2.length - 1; i++)
	{
		min = i;
		for (int j = i + 1; j < test_vals2.length; j++)
		{
			if (test_vals2.list[j] < test_vals2.list[min])
				min = j;
		}
		if (min != i)
		{
			temp = test_vals2.list[i];
			test_vals2.list[i] = test_vals2.list[min];
			test_vals2.list[min] = temp;
		}
	}
	return test_vals2;
}
void printArray_N_store((*file_Sort)(stat_list test_vals3)){
	ifstream outfile;
	for (int a = 0; a < test_vals3.length; a++){
		outfile >> test_vals3.list[a] >> " ";
		cout << test_vals3.list[a] << " ";
		if (a % 10 == 0 && a != 0)cout << endl;
	}

}
/*
if the if statement prints out a 0 use the following

for (int p = 1; p < stat_vals.length; p++){
cout << stat_vals.list[p - 1] << " ";
if (p % 10 == 0 && p != 0)cout << endl;
}
*/

void median_Of_Array((*file_Sort)(stat_list test_vals4)){

	int m1, m2;
	double sum;
	m1 = (1 / 2)*(test_vals4.length);
	m2 = m1 + 1;

	sum = (test_vals4.list[m1]) + (test_vals4.list[m2]);
	test_vals4.median = sum / 2;
	cout << "The median of the array is: " << test_vals4.median << endl;

}
	
Last edited on
Why are you passing a function as a parameter? Why not just call the function?

Ps if you are going to pass as a function you need a std::function or void pointer as far as I was aware but still doesn't make sense.
Last edited on
Im not completely sure I understand you. I did call the function and the reason Im using a function as a parameter is to take the return value from the function without creating a separate variable. Although I tried that as well and I couldn't get to work that way either. Are you saying that a user defined function can't be use as a parameter?
As giblit stated: you really don't need the function as a parameter.

You defined it wrong:
void printArray_N_store((*file_Sort)(stat_list test_vals3));
-> void printArray_N_store(stat_list (*file_Sort)(stat_list test_vals3));

printArray_N_store((&file_Sort)(stat_vals));
-> printArray_N_store(&file_Sort);


What you want is this:
1
2
3
4
5
6
7
stat_list file_Sort(stat_list stat_vals2);
void printArray_N_store(const stat_list &sl); // Note: Not the function but result

int main()
{
...
	printArray_N_store(file_Sort(stat_vals));
Last edited on
I have modified your code Aim4Erudite...hope you find it useful.apologies if it not worked properly.

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
 /* *** Statistical Analysis Program I ***   */


#include<iostream>
#include<fstream>
#include<string>
#include<cstring>

using namespace std;

const int arraySize = 50;

struct stat_list
{
	char list[arraySize];
	int length;
	int maxSize;
	double median;

};
// Function Prototype

stat_list file_Sort(stat_list test_vals2)
{
	//Sorting
	int min;
	double temp;
	for (int i = 0; i < test_vals2.length - 1; i++)
	{
		min = i;
		for (int j = i + 1; j < test_vals2.length; j++)
		{
			if (test_vals2.list[j] < test_vals2.list[min])
				min = j;
		}
		if (min != i)
		{
			temp = test_vals2.list[i];
			test_vals2.list[i] = test_vals2.list[min];
			test_vals2.list[min] = temp;
		}
	}
	return test_vals2;
}

stat_list printArray_N_store(stat_list test_vals3){
	ifstream outfile;
	for (int a = 0; a < test_vals3.length; a++){
	outfile >> test_vals3.list[a];
		cout << test_vals3.list[a];
		if (a % 10 == 0 && a != 0)cout << endl;
	}
	return test_vals3;
}

stat_list median_Of_Array(stat_list test_vals4){

	int m1, m2;
	double sum;
	m1 = (1 / 2)*(test_vals4.length);
	m2 = m1 + 1;
	sum = (test_vals4.list[m1]) + (test_vals4.list[m2]);
	test_vals4.median = sum / 2;
	cout << "The median of the array is: " << test_vals4.median << endl;
	
	return test_vals4;
}

int main()
{
	//variables
	string fileName;
	ifstream inFile;
	ofstream outFile;
	stat_list stat_vals;
	stat_vals.length = 0;
	stat_vals.maxSize = 50;

	//Infile loop
	do
	{
		cout << "Enter file name that contains the data to be sorted: \n";
		cin >> fileName;
		inFile.open(fileName.c_str());
		if (!inFile.is_open())
		{
			cout << "Error opening " << fileName << ", please try again \n";
		}
	} while (!inFile.is_open());

	//Outfile loop
	do
	{
		cout << "Enter the name of the file to store the sorted numbers: \n";
		cin >> fileName;
		outFile.open(fileName.c_str());
		if (!outFile.is_open())
		{
			cout << "Error opening " << fileName << ", please try again \n";
		}
	} while (!outFile.is_open());

	//filling array with values
	
	inFile >> stat_vals.list[stat_vals.length];
	while (inFile && stat_vals.length < stat_vals.maxSize) {
		stat_vals.length++;
		inFile >> stat_vals.list[stat_vals.length];
	}

	//Sort array
	file_Sort(stat_vals);
	printArray_N_store(stat_vals);
	median_Of_Array(stat_vals);

	outFile.close();
	inFile.close();

	return 0;
}
Im sorry guys Im trying all of the things your telling me to the best of my ability and I keep getting the following:
- Im using Visual Studio and Ive even tried on a separate IDE (called EBE) provided by the school and no luck.
This is the first error with the visual studio,

source.cpp(74): error C2664: 'void printArray_N_store(stat_list (__cdecl *)(stat_list))' : cannot convert argument 1 from 'stat_list' to 'stat_list (__cdecl *)(stat_list)'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
Last edited on
There's an easy way to make up a typedef that matches a given function. Say we want to make up a typedef for:
 
stat_list file_Sort(stat_list stat_vals2);
it would be:
 
typedef stat_list file_Sort_t(stat_list stat_vals2);


There's one detail we need to look at before we define printArray_N_store is the function it takes takes a parameter itself, a stat_list passed by value. So we need to pass that parameter in too, but we'll pass it in by const ref.

So the definition of printArray_N_store becomes:
 
void printArray_N_store_t(file_Sort_t* func, const stat_list& arg);


The code that uses those definitions become:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main()
{
	// ...
	printArray_N_store(file_Sort, stat_vals);
	// ...
}

void printArray_N_store(file_Sort_t* func, const stat_list& arg)
{
	stat_list test_vals3 = func(arg);

	ifstream outfile(/* need a filename */);
	for (size_t a = 0; a != test_vals3.length(); ++a)
	{
		outfile >> test_vals3.list[a] >> " ";
		cout << test_vals3.list[a] << " ";
		if (a % 10 == 0 && a != 0)
			cout << endl;
	}
}

Last edited on
Thank you all for your help, I apologize for the late response. Im in the last two week of summer school and they are laying it on thick. Also I have a two year old and five year old, so I don't have much free time. I plan to use your advice tomorrow. Thank you all again.
Topic archived. No new replies allowed.