How can I enter data into a struct after I pass by ref to a function?

Hello,

I am trying to enter data into a data structure after passing it by ref to a function. When I use a for loop to refer to the specific struct index, I just get a load of errors. I refer to it as I would any array address in the loop.

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
// Exercise13.3_4.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

struct employee
{
	int id_number;
	string name;
	double rate;
	int hours;
};

static const int no_of_records = 6;

double gross_pay(employee &);
void display_records(employee &);
void enter_data(employee &);

void enter_data(employee &records)
{
	for (int i = 0; i < no_of_records; i++)
	{
		cout << "Enter the ID" << endl;
		cin >> records[i].id_number;

		cout << "Enter the Name" << endl;
		cin >> records[i].name;

		cout << "Enter the rate" << endl;
		cin >> records[i].rate;

		cout << "Enter the hours worked" << endl;
		cin >> records[i].hours;
	}
}

void display_records(employee &records)
{
	for (int i = 0; i < no_of_records; i++)
	{
		cout << "The id no is " << records[i].id_number << endl;
		cout << "The id no is " << records[i].name<< endl;
		cout << "The id no is " << records[i].rate<< endl;
		cout << "The id no is " << records[i].hours << endl;
	}

}

int main()
{
	employee employee_records[no_of_records];
	enter_data(employee_records);
	display_records(employee_records);
    return 0;
}


Next time, you should post what those errors are, with corresponding line numbers.

employee is the type you defined. Treat it just like int or any other type, as far as syntax goes.

int arr[6]; is an array of ints. How would you normally pass an array of ints into a function?

employee employee_records[6]; is an array of employees. Not a single employee, as Employee& would suggest.

An array decays into a pointer to that array when passed into a function (although the language is nice and still lets you use [] to denote that it's meant to be a pointer to an array instead of just a pointer, but the syntax is equivalent to "employee* records").
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void enter_data(employee records[])
{
	for (int i = 0; i < no_of_records; i++)
	{
		cout << "Enter the ID" << endl;
		cin >> records[i].id_number;

		cout << "Enter the Name" << endl;
		cin >> records[i].name;

		cout << "Enter the rate" << endl;
		cin >> records[i].rate;

		cout << "Enter the hours worked" << endl;
		cin >> records[i].hours;
	}
}
Last edited on
Hello,

Thanks very much! I was under the impression that the function would create a copy of the array in order to execute any instructions inside it. Hence I thought I should pass by reference. But I should try to use pointer notation to perhaps do it again?
Here is the complete code, including the gross pay calculator. Thanks again!

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
// Exercise13.3_4.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

struct employee
{
	int id_number;
	string name;
	double rate;
	int hours;
};

static const int no_of_records = 6;

double gross_pay(employee []);
void display_records(employee []);
void enter_data(employee []);

void enter_data(employee records[])
{
	for (int i = 0; i < no_of_records; i++)
	{
		cout << "Enter the ID" << endl;
		cin >> records[i].id_number;

		cout << "Enter the Name" << endl;
		cin >> records[i].name;

		cout << "Enter the rate" << endl;
		cin >> records[i].rate;

		cout << "Enter the hours worked" << endl;
		cin >> records[i].hours;
	}
}

void display_records(employee records[])
{
	
	cout << setiosflags(ios::left);
	for (int i = 0; i < no_of_records; i++)
	{
		cout << setw(10) << records[i].name
			<< setw(10) << records[i].id_number
			<< setw(10) << records[i].hours
			<< setw(10) << records[i].rate;
	}
}

double gross_pay(employee records[])
{
	double total = 0.0;

	for (int i = 0; i < no_of_records; i++)
	{
		total = total + (records[i].hours * records[i].rate); 
	}
	
	return total;
}

int main()
{
	employee employee_records[no_of_records];
	enter_data(employee_records);
	display_records(employee_records);

	cout << "The total pay is" << gross_pay(employee_records) << endl;


	system("pause");
	
    return 0;
}

Glad it works. Indeed, raw arrays are an exception to what might be intuitive because it actually passes just a pointer to the array instead of making a copy of each element.

If you used library data structures such as std::array or std::vector, you could have an object that behaves as you wish, where a copy is made if you don't pass it by reference.
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
#include <iostream>
#include <array>

void work_on_copy(std::array<int, 3> arr_copy)
{
	arr_copy[0] = 42;
}

void work_on_ref(std::array<int, 3>& arr_ref)
{
	arr_ref[0] = 42;
}

void print_array(const std::array<int, 3>& arr)
{
	for (int i = 0; i < 3; i++)
	{
		std::cout << arr[i] << " ";
	}
	std::cout << std::endl;
}

int main() {
	std::array<int, 3> arr;
	arr[0] = 6;
	arr[1] = 5;
	arr[2] = 4;
	print_array(arr);
	
	work_on_copy(arr);
	print_array(arr); // Didn't change main array
	
	work_on_ref(arr);
	print_array(arr); // Correctly changed main array
}


You could make it a reference to a single employee object, but employee_records is not a single employee, it is an array. You'd have to pass employee_records[0] instead of employee_records itself to be able to pass a reference to an employee. But that's really complicated to work as array, since you'd have to then take the address of the reference to get the pointer, and then increment the pointer. Which is a mess to work with, so I don't suggest doing that.
Last edited on
Topic archived. No new replies allowed.