return struct array from a function

Hi,
This program takes in employees details like name,title,salary etc and should return the structure array.I am not being able to return the array of structure.I think I am doing something wrong on declaring the type of the function.

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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <stdlib.h>
#include "genlib.h"
#include "strutils.h"
#include <simpio.h>

//structure

struct employeeRecordT{
	
	string name;
	string title;
	string ssnum;
	double salary;
	int withholding;
	
	
	
};

	
	


employeeRecordT GetPayroll(int n);

//main program
int main(){


	

	cout<<"How many employees:";
	
	
	int n=GetInteger();
	
	
	

	GetPayroll(n);
	

	
	
}
	


//GetPayroll function which takes in details and should return the details

employeeRecordT GetPayroll(int n){
	
		
	string name=" ";
	string title=" ";
	string ssnum=" ";
	double salary=0.0;
	int withholding=0;
	
	
	
	struct employeeRecordT{
		
		string name;
		string title;
		string ssnum;
		double salary;
		int withholding;
		
		
		
	};
	
	employeeRecordT *pointer = new employeeRecordT[n];
	 
		
		
		
	for(int i=0;i<n;i++){
			
		cout<<"Employee #"<<i+1<<":"<<endl;
		
		cout<<"  "<<"Name:";
	
		name=GetLine();
		
		
		
		pointer[i].name=name;
	
		cout<<"  "<<"Title:";
	
		title=GetLine();
		
		pointer[i].title=title;
		
		
		cout<<"  "<<"SSNum:";
		
		ssnum=GetLine();
		
		pointer[i].ssnum=ssnum;
		
		
		
		cout<<"  "<<"Salary:";
		
		salary=GetReal();
		
		
		
		pointer[i].salary=salary;
		
		
		cout<<"  "<<"Withholding:";
		
		withholding=GetInteger();
		
		pointer[i].withholding=withholding;
	
	
	}
	

	 return pointer;	
	}







Ohk just to try and get up to speed with what you want. is to return the information which you just inserted by using getpayroll funciton.

1) I'm not sure about creating another "struct employeeRecordT" inside your get payroll function you already did that part at the very top. Will someone also inlighten me why that worked the structure iside a method i mean.

2) You using pointers and the reseaon your function is not returning or even compiling is because it is being confused.
if you using pointers and assigning to variables try e.g. pointer->name = name ... if not then dont instantiate pointer as *pointer
just let it be pointer without the astrict then you can use pointer[i].name = name. its all about speed.
have a look at your function name you telling the compiler you will return a structure which is not a pointer, but in the end you
returning a pointer.

I know I know i just confused you with pointer pointer. I'm not that good in explaining but i try heres the diffrence

employeeRecordT* GetPayroll(int n) <---------------------------Note the * by the function
employeeRecordT *pointer = new employeeRecordT[n];
return pointer

whereas
employeeRecordT GetPayroll(int n) <---------------------------No astrict
employeeRecordT pointer = new employeeRecordT[n]; <--------------------------Same goes here when instantiating
return pointer

3) With those two things out of the way you good to for loop your structure to show output. Hope I'm clear enough.

Last edited on
Its working boss,thank you
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
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <stdlib.h>
#include "genlib.h"
#include "strutils.h"
#include <simpio.h>

//structure

struct employeeRecordT
	{
		string name;
		string title;
		string ssnum;
		double salary;
		int withholding;
	};

employeeRecordT GetPayroll(int n);

//main program
int main()
	{
		cout<<"How many employees:";	
		int n=GetInteger();
		GetPayroll(n);	
	}
	


//GetPayroll function which takes in details and should return the details

employeeRecordT GetPayroll(int n)
	{
		string name=" ";
		string title=" ";
		string ssnum=" ";
		double salary=0.0;
		int withholding=0;
	
		employeeRecordT pointer = {0};		
		for(int i=0;i<n;i++)
			{
			
				cout<<"Employee #"<<i+1<<":"<<endl;		
				cout<<"  "<<"Name:";
				name=GetLine();						
				pointer[i].name=name;
				cout<<"  "<<"Title:";	
				title=GetLine();		
				pointer[i].title=title;
				cout<<"  "<<"SSNum:";
				ssnum=GetLine();		
				pointer[i].ssnum=ssnum;		
				cout<<"  "<<"Salary:";
				salary=GetReal();
				pointer[i].salary=salary;
				cout<<"  "<<"Withholding:";
				withholding=GetInteger();
				pointer[i].withholding=withholding;
			}
	

		return pointer;	
	}

string GetLine()
{
	string m_strTemp  = "" ; 
	cout<<"\n Enter the string ";
	cin>> m_strTemp; 
	return m_strTemp; 
}
double GetReal()
{
	double m_dvalue  = 0.0 ;
	cout<<"\n Enter the value " ; 
	cin>>m_dvalue ; 
	return m_dvalue ; 
}

int GetInteger()
{
	int m_nvalue  = 0 ; 
	cout<<"\n  Enter the value ";
	cint>>m_nvalue ; 

	return m_nvalue ;  
}
Topic archived. No new replies allowed.