program passing structure arrays with functions

Hello, I am creating a program that that passes structure arrays through functions. I Am trying to get user input to add information to a strut variable using a function. I am getting an error message on the get user input line that reads " field'employee_id' cannot be resolved. Any suggestions to fix this error and BTW the program is not yet finished.
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
#include <iostream>
#include <iomanip>
using namespace std;
const int num_workers = 10;
struct Payroll
{
	int employee_id[num_workers];
	double hours_worked[num_workers];
	double pay_rate[num_workers];
	double total_pay[num_workers];


};

void get_jd(Payroll[], int);
void get_jd(Payroll a[], int size)
{
	for(int size = 0 ; size < num_workers; size ++)
	{
      cin >> a.employee_id[size];
	}
}
int main() {
	Payroll admin[num_workers];
	Payroll office[num_workers];
	Payroll field[num_workers];
	cout << " enter employee id numbers for 10 employee" << endl;
	get_jd( admin,num_workers);
	return 0;
}
In your get_jd function, a is not a Payroll object. a is an array of Payroll objects. To get a single Payroll object from the array, you use:

a[index].

To access the member of that Payroll object, you use:

a[index].element

Why do you have an array of payrolls, of size num_workers, when each payroll then contains several arrays that are also of size num_workers? That seems redundant to me.
Last edited on
ok thanks got it working and i was assuming that i declare the arrays in payroll before my get_jd function
Um... I'm not quite sure what you mean by that. But the point is, you need to think clearly about how you organize your data. Do you want one Payroll, which contains data for all workers? Or do you want lots of Payrolls, one for each worker?
I want to organize lots of Payrolls for each worker. The problem I am running into now is when I am getting user input the program keeps running, like there is an infinite loop going on. Would the cin ignore statement fix this?
I want to organize lots of Payrolls for each worker.

You want each worker to have lots of Payrolls?

Or do you want each worker to have one Payroll?

The problem I am running into now is when I am getting user input the program keeps running, like there is an infinite loop going on. Would the cin ignore statement fix this?

Without seeing the current version of the code, there's really no way we can do anything other than guess.
Last edited on
Topic archived. No new replies allowed.