Call by reference

Hello,
I have a file data.txt that has these set of numbers:
126534 9 8 10
321345 7 3 5
324341 9 9 9
I'm trying to create a call by reference that will use a function called ProcessARow to calculate Min,Max and Average in a table like this
Std-Id A1 A2 A3 Min Max Average
----------------------------------S-------------------------------------
126534 9 8 10 8 10 9.0
321345 7 3 5 3 7 5.0
324341 9 9 9 9 9 9.0

I keep getting errors
Error 2 error LNK2019: unresolved external symbol
Error 3 error LNK1120: 1 unresolved externals
Not sure what I'm doing wrong as the forum suggests I'm a newbie.



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
  #include <fstream>
#include <iostream>

using namespace std;



void ProcessARow(int x, int y, float z);



int main()
{
	ifstream fin;
	fin.open("data.txt");
	int id, A1, A2, A3;

	cout << "Std-Id \tA1 \tA2 \tA3 \t Min \t Max \t Avg\n";
	cout << "---------------------S-------------------------------\n";
	while (fin >> id >> A1 >> A2 >> A3){
			
		int max = A1;
		int min = A1;
		float avg = A1 + A2 + A3 / 3.0;

		
			if (A2 > max)
				max = A2;
			if (A3 > max)
				max = A3;
			if (A2 < min)
				min = A2;
			if (A3 < min)
				min = A3;
			ProcessARow(min, max, avg);
			cout << id << "\t" << A1 << "\t" << A2 << "\t" << A3 << "\t" << min << "\t" << max << "\t" << avg << endl;
			system("pause");
		}

}
1) You are passing by copy.
2) You never told the compiler what the function does.
Not sure what you mean?
void ProcessARow(int &x, int &y, float &z); This is passing by reference.
void ProcessARow(int x, int y, float z); What does this do??
like this?

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
int ProcessARow(int &max, int &min, int &avg);

int main()
{
	ifstream fin;
	fin.open("data.txt");
	int id, A1, A2, A3;

	cout << "Std-Id \tA1 \tA2 \tA3 \t Min \t Max \t Avg\n";
	cout << "---------------------S-------------------------------\n";

	while (fin >> id >> A1 >> A2 >> A3)
 {
		int max = A1;
		int min = A1;
		if (A2 > max)
			max = A2;
		if (A3 > max)
			max = A3;
		if (A2 < min)
			min = A2;
		if (A3 < min)
			min = A3;
		float avg = A1 + A2 + A3 / 3.0;
		cout << id << "\t" << A1 << "\t" << A2 << "\t" << A3 << "\t" << min << "\t" << max << "\t" << avg << endl;
		
		
		
	}

	

	system("pause");
}

Last edited on
You need to read up on functions I think. You are not understanding how to setup them. Basically you want something like:

1
2
3
4
5
6
7
8
9
10
11
12
int function(/*parameters type & name(suggested not required)*/); //prototype

int main()
{
    function(/*parameter variables/values*/); //calling the function
}

int function(/*parameters type & name(required)*/ ) //declaration
{
    //do something
    //you can use the parameter variables
}


http://www.cplusplus.com/doc/tutorial/functions/

You've removed the call to ProcessARow which has probably eliminated the undefined external. Since you're not calling it, you don't need the function prototype at line 1.

giblit wrote:
You never told the compiler what the function does.

What giblit meant is that you provided a function declaration, but your never wrote the function. That is what the linker tried to tell you with the "undefined external" message. i.e. Where is the implementation for ProcessARow?

This is my latest however I'm getting new errors

redefinition of formal parameter max,min???

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
#include <fstream>
#include <iostream>

using namespace std;



void ProcessARow(int id,int A1,int A2,int A3,int& max, int& min, float& avg);
int main()
{
	ifstream fin;
	fin.open("data.txt");
	int id, A1, A2, A3, max, min;
	float avg;
	cout << "Std-Id \tA1 \tA2 \tA3 \tMin \tMax \tAvg\n";
	cout << "--------------------S------------------------------\n";
	while (fin >> id >> A1 >> A2 >> A3)
	{
		ProcessARow(id, A1, A2, A3, max, min, avg);
	}
}
	void ProcessARow(int id, int A1, int A2, int A3, int &max, int &min, float &avg)
	{
		int max = A1;
		int min = A1;
		if (A2 > max)
			max = A2;
		if (A3 > max)
			max = A3;
		if (A2 < min)
			min = A2;
		if (A3 < min)
			min = A3;
		avg = (A1 + A2 + A3) / 3.0;
		cout << id << "\t" << A1 << "\t" << A2 << "\t" << A3 << "\t" << min << "\t" << max << "\t" << avg << endl;
		return;

		system("pause");
	}

1
2
		int max = A1;
		int min = A1;
You should remove the int from both and use the ones passed via the parameters.
I fixed it needed to fix line 24,25
Thanks so much giblit
Topic archived. No new replies allowed.