Trouble with passing arrays of structures to functions

I do not understand where I'm going wrong in syntax here.

I am trying to use an array of structures, but it will only compile when I use a structure variable. When I put a [10] after empArray I get,

error C2664: 'empNamef' : cannot convert parameter 1 from 'EmpData (*)[50]' to 'EmpData *'

error C2228: left of '.empNameDatf' must have class/struct/union

I have searched here and the web but can't see my error. Thanks as always.

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
#include "stdafx.h"
#include <iostream>
#include <cstring>
#include <iomanip>
#include <fstream>

using namespace std;

const int size = 50;

struct EmpData
{
	char empNameDatf[size];
	char empNameDatl[size];
	char stat[size];
	int empIdDat[size];
	double empHrsWrk[size];
	double empPayDat[size];
	double totalPay[size];
};

void dispMenu(int &choice);

void empNamef(struct EmpData *);


int main()
{
	//dec vars
	string fName, lName, stat;
	int choice, index; 
	double hrsWrk, payRt, empId;

	ofstream outFile;
	ifstream inFile;

	EmpData empArray[10];

	//intro
	cout << '\n';
    cout << "Hello, this program will help you run your payroll and find employee payroll information. \n";
	cout << "Please select one of the options from the menu to start: \n";
	cout << '\n';

do
{
	//call menu
	dispMenu(choice);

	//switch if program not ended
	if (choice != 8)
	{
	    switch(choice)
	    {
	     //enter employee name case
		 case 1: empNamef(&empArray);
			     cout << "Name: " << empArray.empNameDatf << endl;
		     break;
	     //enter employee ID number case
		 case 2: ;
			 break;
	     //enter employee hours case
		 case 3: 
			 break;
	     //enter employee pay case
		 case 4: 
			 break;
	     //employee report sorted case
		 case 5: 
			 break;
	     //total employee pay case
	     case 6: 
			 break;
	     //employee search case
		 case 7: 
			 break;
	    }
	}

}while(choice != 8);

    return 0;
}

//disp menu function
void dispMenu(int &choice)
{
	cout << "\n";
	cout << "Enter employee information Below: \n";
	cout << "\n";
	cout << " Enter 1: Enter employee Name \n";
	cout << " Enter 2: Enter employee ID number  \n";
	cout << " Enter 3: Enter employee Hours worked  \n";
	cout << " Enter 4: Enter employee Payrate  \n";
	cout << '\n';
	cout << "Generate a Payroll Report Below: \n";
	cout << '\n';
	cout << " Enter 5: Complete employee payroll information sorted by ID number  \n";
	cout << " Enter 6: Calculate Payroll  \n";
	cout << " Enter 7: Search for employee Payroll Information by ID number \n";
	cout << " Enter 8: Quit Program  \n";
	cout << '\n';
	cin  >> choice;
	cout << '\n';
}

void empNamef(EmpData *s)
{
	
	cout << "Please enter employee #" << "'s First name: \n";
	cin.ignore();
	cin.getline(s->empNameDatf, size);

	cout << '\n';

}





Last edited on
I made it easier to read from the last post.
 
case 1: empNamef( empArray );


1
2
3
  
// note that you need some index in order for the below code to work
cout << "Name: " << empArray[ index ].empNameDatf << endl;
Thank you! But why do I not use the & operator in the call?

I changed this code, but if I add s->empNameDatf[index] in the function definition it wont compile and if I don't it compiles but only reads part of the first element.

1
2
3
4
5
case 1: empNamef(empArray, size, index);
			     for(index = 0; index < 3; index++)
				 {
			     cout << "Name: " << empArray[index].empNameDatf << endl;
				 }


1
2
3
4
5
6
7
8
9
10
11
12
void empNamef(struct EmpData *s, int size, int index)
{
	
	for(index = 0; index < 3; index++)
	{
	cout << "Please enter employee #" << "'s First name: \n";
	cin.ignore();
	cin.getline(s->empNameDatf, size);
	}

	cout << '\n';
}
Last edited on
But why do I not use the & operator in the call?

actually empNamef( empArray ); means that you pass the address of empArray[ 0 ] to empNamef function, that call is only valid if you want to pass empArray[ 0 ] to the function, for other indexes you will need
empNamef( &empArray[ index ] )


I changed this code, but if I add s->empNameDatf[index] in the function definition it wont compile and if I don't it compiles but only reads part of the first element.

no, you don't need that for loop, but instead rethink how you will design the menu and entry of data.

your code flow can be interpreted as follow :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main ()
{
EmpData empArray[ 10 ];

do
    // print menu

    // enter choice
    case 1 :
        // read employee name by passing &empArray[ ? ] to it
        // print it
    case 2 :
        // read ID number by passing &empArray[ ? ] to it
        // print it
    case 3 :
        // read work hour by passing &empArray[ ? ] to it
    ...
}


The problem w/ this is the reading of data is misleading, how would you know what element of the array will you pass to the function ?

A solution to this is to implement again your menu and group the reading of information under 1 case :
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
int main
{
EmpData empArray[ 10 ];
int index = 0; // <- Note: we now have an index to work w/

do
    //-- group the reading of data into 1 case, much like :
    //-- 1. Enter Information( name, id, ... ) <- here reading of info is move into "1 case", so you don't mess up with indexes
    //---//--//--//
    //-- 2. print info's
    //-- 3. Calculate Payroll
    //-- 4. etc...
    //-- 5. exit

    cin >> choice;
    switch ( choice ) {
    case 1:
        //-- call function to store the name
        empNamef( &empArray[ index ] );

        //-- call function to store ID
        empIdf( &empArray[ index ] );

        //-- read other infos

        // increment index so in the next "case 1" the information will be stored on the next element of the array :
        index++;
        if( index == 10 ) return 0; // if index == the size of the array, exit the program ( to prevent overflows )
    break;
    case 2 :
        //-- print infos
        for( int i = 0; i < index; i++ ) {
            std::cout << empArray[ i ].name << " " << empArray[ i ].ID << etc...
        }
    break;
    case 3 ...
    case 4...

    case 5 :
        return 0;
    }

}
Last edited on
Thanks again! I sizzled my brain but good trying to figure it out last night.

Why does it always makes so much sense after you're shown the way lol
...
Last edited on
Topic archived. No new replies allowed.