Failed to pass struct to function

I was wondering how it works actually. Any point out for me will be helpful.
Thanks in advance.

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
#include <iostream.h>

	struct EmpRec {
        char empCode[5];
        char empname[20];  
        int age;
        float salary;
        int yearJoined;
	};
	

int GiveCars(struct EmpRec Emp[10],int NumOfEmp);

int main(){
	
        struct EmpRec Emp[10];
	
	int NumOfEmp,count=0;

	cout<<"Please enter the number the records to be kept(max=10) : ";
	cin>>NumOfEmp;
	
	while(count<NumOfEmp){
		cout<<"Employee Code : ";
		cin>>Emp[count].empCode;
		cout<<"Employee Name : ";
		cin>>Emp[count].empname;
		cout<<"Employee Age : ";
		cin>>Emp[count].age;
		cout<<"Employee Salary : ";
		cin>>Emp[count].salary;
		cout<<"Year Joined : ";
		cin>>Emp[count].yearJoined;
		cout<<"---------------------------------"<<endl;
		
		count++;
	}

	cout<<"The numbers of employee entitled for the car : "<<GiveCars(struct EmpRec Emp,NumOfEmp)<<endl;

	return 0;
}


int GiveCars(struct EmpRec Emp[10],int NumOfEmp){
	int count=0,entitled=0,totalyear;

	while(count<=NumOfEmp){
		totalyear=2010-Emp[count].yearJoined;
		if (totalyear>10 )
			entitled++;
		else
			;
		count++;
	}


	return entitled;
}



Error Message:
C:\Documents and Settings\XPMUser\Desktop\Cpp1.cpp(38) : error C2144: syntax error : missing ')' before type 'EmpRec'
C:\Documents and Settings\XPMUser\Desktop\Cpp1.cpp(38) : error C2660: 'GiveCars' : function does not take 0 parameters
C:\Documents and Settings\XPMUser\Desktop\Cpp1.cpp(38) : error C2059: syntax error : ')'

Anyone?
Last edited on
Hey there...

Declare your EmpRec:
typedef EmpRec Emp[10];

And when you're done with that... You can actually use EmpRec as a synonym for Emp[10]....
(You don't have to redefine it in the function again) ;-)
About my codes, I've check over and over again...I don't quite understand what is the problem.

Hey there...

Declare your EmpRec:
typedef EmpRec Emp[10];

And when you're done with that... You can actually use EmpRec as a synonym for Emp[10]....
(You don't have to redefine it in the function again) ;-)


Can you elaborate more on it? I don't quite understand.

*Error message posted.
I'm sorry... I must have misread :-/
Please ignore my previous message...

on line 39 try something like this:

...<<GiveCars(Emp,NumOfEmp)<<...
btw....
After the input you just assume that the person typing the number of employees actually puts a number less than 10... You should check again in your program....

After the std::cin either put a

1
2
3
if (NumOfEmp > 10) {
     NumOfEmp = 10;
}


Or ask again if the NumOfEmp is bigger than 10....

Same thing about the name and code...

You might want to look at something like std::vector or std::list... and for the char arrays std::strings ;-) good luck
attaboy : Thanks a lot, man. You've saved my day. Will look more into vector or list later on. Thanks again =)
Topic archived. No new replies allowed.