Adding employee names in function arranged program

The program that I have written below is working. It calculates a number of payroll type variables and most recently the net pay average (All this is working 100%).

My problem is that I need to add first and last employee names to the program but every time I do this I end up ruining the working program. I've successfully added first and last names to similar programs in the past(ones that make use of an array and while loop) but never to a program that uses functions (I always have problems doing this).

My question is what codes do I use to add a first and last name variable and where do I put those codes within my program so it runs/displays correctly.

I think the coding must be similar to what I've used in the past charr and of course firstname[i] and lastname[i] lines. Any help would be great.

Thanks
_______________________________________________________________________
Current Input File:
1645 40 25.00
8932 40 20.00
7104 40 12.50
2816 40 26.00
5387 40 21.00
6780 40 13.50
5641 40 11.25

9000 60 25.00
9001 50 20.00
9002 55 23.00

Ideal input file (would include names) Example.
1645 Bob Smith 40 25.00
_______________________________________________________________________
Syntax:
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
139
140
141
142
143
144
145
146
#include <iostream>
#include <iomanip>
#include<fstream>
using namespace std;

//function prototypes
int readalldata(long int[], int[], float[], const int);
void findovertimehours(int[], int[], int);
void findovertimepay(int[], float[], float[], int);
void findregularhours(int[], int[], int);
void findregularpay(int[], float[], float[], int);
void findgrosspay(float[], float[], float[], int);
void findtaxrate(float[], float[], int);
void findtaxamount(float[], float[], float[], int);
void findnetpay(float[], float[], float[], int);
void printalldata(long int[], int[], float[], float[], float[], float[], float[], int);

int main(){
	const int MAXSIZE = 100;   //for maximum of 100 employees

	//decleration of variables
	int n;
	long int id[MAXSIZE];
	int hoursworked[MAXSIZE], overtimehours[MAXSIZE];
	int regularhours[MAXSIZE];
	float hourlyrate[MAXSIZE], regularpay[MAXSIZE],
		overtimepay[MAXSIZE], grosspay[MAXSIZE];
	float taxrate[MAXSIZE], taxamount[MAXSIZE], netpay[MAXSIZE];

	//function calls
	n = readalldata(id, hoursworked, hourlyrate, MAXSIZE); //get all data
	findovertimehours(hoursworked, overtimehours, n);
	findovertimepay(overtimehours, hourlyrate, overtimepay, n);
	findregularhours(hoursworked, regularhours, n);
	findregularpay(regularhours, regularpay, hourlyrate, n);
	findgrosspay(regularpay, overtimepay, grosspay, n);
	findtaxrate(grosspay, taxrate, n);
	findtaxamount(grosspay, taxamount, taxrate, n);
	findnetpay(grosspay, netpay, taxamount, n);
	printalldata(id, hoursworked, hourlyrate, overtimepay,
		grosspay, taxamount, netpay, n);
	return 0;
}//MAIN
//fUNCTION DEFINITIONS
int readalldata(long int id[], int hoursworked[], float hourlyrate[], int n){
	ifstream fin("employee.txt");
	n = 0;

	while (fin >> id[n] >> hoursworked[n] >> hourlyrate[n]) n++;

	fin.close();
	return n;
}//READALLDATA

void findovertimehours(int hoursworked[], int overtimehours[], int n){
	for (int i = 0; i<n; i++){
		if (hoursworked[i]>40) overtimehours[i] = hoursworked[i] - 40;
		else overtimehours[i] = 0;
	}//FOR
}//FINDOVERTIMEHOURS

void findovertimepay(int overtimehours[], float hourlyrate[],
	float overtimepay[], int n){
	for (int i = 0; i<n; i++){
		overtimepay[i] = overtimehours[i] * hourlyrate[i] * 1.5;
	}//FOR
}//FINDOVERTIMEPAY

void findregularhours(int hoursworked[], int regularhours[], int n){
	for (int i = 0; i<n; i++){
		if (hoursworked[i]>40) regularhours[i] = 40;
		else regularhours[i] = hoursworked[i];
	}//FOR
}//FINDREGULARHOURS

void findregularpay(int regularhours[], float regularpay[],
	float hourlyrate[], int n){
	for (int i = 0; i<n; i++){
		regularpay[i] = regularhours[i] * hourlyrate[i];
	}//FOR
}//FINDREGULARPAY
void findgrosspay(float regularpay[], float overtimepay[],
	float grosspay[], int n){
	for (int i = 0; i<n; i++){
		grosspay[i] = regularpay[i] + overtimepay[i];
	}//FOR
}//FINDGROSSPAY

void findtaxrate(float grosspay[], float taxrate[], int n){
	for (int i = 0; i<n; i++){
		if (grosspay[i]>4000.00) taxrate[i] = 0.40;
		else if (grosspay[i]>3000.00) taxrate[i] = 0.30;
		else if (grosspay[i]>1000.00) taxrate[i] = 0.20;
		else taxrate[i] = 0.10;
	}//FOR
}//FINDTAXRATE

void findtaxamount(float grosspay[], float taxamount[],
	float taxrate[], int n){
	for (int i = 0; i<n; i++){
		taxamount[i] = grosspay[i] * taxrate[i];
	}//FOR
}//FINDTAXAMOUNT

void findnetpay(float grosspay[], float netpay[], float taxamount[], int n){
	for (int i = 0; i<n; i++){
		netpay[i] = grosspay[i] - taxamount[i];
	}//FOR
}//FINDNETPAY
int i = 0;
void printalldata(long int id[], int hoursworked[], float hourlyrate[],
	float overtimepay[], float grosspay[], float taxamount[],
	float netpay[], int n){
	float totalNetPay = 0;
	cout << setw(48) << "PAYROLL INSTITUTE" << endl;
	cout << setw(40) << "106 EASY WAYS LANE" << endl;
	cout << setw(43) << "PLEASANTVILLE N.Y. 11068" << endl;
	cout << " " << endl << endl;
	cout << "============================================================ " << endl;
	cout << fixed << setprecision(2);
	cout << "EMP ID" << setw(7)
		<< "HOURS" << setw(6)
		<< "RATE" << setw(10)
		<< "OVERPAY" << setw(11)
		<< "GROSSPAY" << setw(8)
		<< "TAX" << setw(11)
		<< "NETPAY" << endl;

	for (i = 0; i<n; i++){
		totalNetPay += netpay[i];
		cout << "" << id[i] << setw(7) 
			<< hoursworked[i] << setw(9)
			<< hourlyrate[i] << setw(7)
			<< overtimepay[i] << setw(12)
			<< grosspay[i] << setw(10)
			<< taxamount[i] << setw(10) 
			<< netpay[i] << endl;
	}//FOR
	cout << "============================================================ " << endl;
	cout << "AVERAGE NETPAY FOR ALL EMPLOYEES IS:" << endl;
	cout << setw(18) << totalNetPay / i << endl;
	cout << endl;
	system("PAUSE");
}//PRINTALLDATA
//end source code
}
Last edited on
Please use code tags when posting code, to make it readable:

http://www.cplusplus.com/articles/z13hAqkS/

Note that the formatting will add the line numbers for you; there's no need to type them yourself.
Wow thanks that's a whole lot easier then trying to type out all those numbers by hand as well. I'll take any advice I can get.
Last edited on
Thanks for editing your post to add code tags.

Adding first and last name is really no different than what you've already done. You will need an array to hold first and last names. Then modify your input statement to add first and last names to it.

line 29:
1
2
 
  string fname[MAXSIZE], lname[MAXSIZE];


line 49:
1
2
while (fin >> id[n] >> fname[n] >> lname[n] >> hoursworked[n] >> hourlyrate[n]) 
    n++;


You will also need to add these arrays as arguments to your readalldata function.

Have you learned structs yets? It would be a lot clearer to place these data items in a struct, then have one struct for each employee.
1
2
3
4
5
6
7
8
9
10
11
12
13
struct empinfo_t 
{   long id;
    int hoursworked;
    int overtimehours;
    int regularhours;
    float hourlyrate;
    float regularpay;
    float overtimepay;
    float grosspay;
    float taxrate;
    float taxamount;
    float netpay;
} empinfo[MAXSIZE];



Last edited on
I'd love to have used struct but we haven't covered it yet (it's in the next chapter). It seems like is a lot more organized then trying to do things the current way.
AnonAbstraction is on point with his suggestion. Just remember to write string fname and string lname into the struct so when you read the input file each name is stored with the values supplied.
So I made the changes to the program as you stated...

Line 30
Syntax:
1
2
string fname[MAXSIZE], lname[MAXSIZE];
}


and Line 50
Syntax:
1
2
3
while (fin >> id[n] >> fname[n] >> lname[n] >> hoursworked[n] >> hourlyrate[n]) 
    n++;
}


Then I tried to add the arguments to the read all data function and I ran into problems again as I'm not 100% sure of what I'm doing (as far as functions go). Since I'm just starting out in c++ I'm still learning functions and most of what I have as a program comes from exercises out of a work book.

My updated program looks like this...(This is the program with only your specifically mentioned changes to it. I've made more changes which are listed below but since you know what your doing and I don't I'd love to go step by step if you are willing to do so)
_____________________________________________________________________
Syntax:
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
139
140
141
142
143
144
145
146
147
148
149
150
#include <iostream>
#include <iomanip>
#include<fstream>
#include <string>
using namespace std;
string fname[MAXSIZE], lname[MAXSIZE];
//function prototypes
int readalldata(long int[], int[], float[], const int);
void findovertimehours(int[], int[], int);
void findovertimepay(int[], float[], float[], int);
void findregularhours(int[], int[], int);
void findregularpay(int[], float[], float[], int);
void findgrosspay(float[], float[], float[], int);
void findtaxrate(float[], float[], int);
void findtaxamount(float[], float[], float[], int);
void findnetpay(float[], float[], float[], int);
void printalldata(long int[], int[], float[], float[], float[], float[], float[], int);

int main(){
	const int MAXSIZE = 100;   //for maximum of 100 employees

	//decleration of variables
	int n;
	long int id[MAXSIZE];
	int hoursworked[MAXSIZE], overtimehours[MAXSIZE];
	int regularhours[MAXSIZE];
	float hourlyrate[MAXSIZE], regularpay[MAXSIZE],
		overtimepay[MAXSIZE], grosspay[MAXSIZE];
	float taxrate[MAXSIZE], taxamount[MAXSIZE], netpay[MAXSIZE];
	string fname[MAXSIZE], lname[MAXSIZE];
	//function calls
	n = readalldata(id, hoursworked, hourlyrate, MAXSIZE); //get all data
	findovertimehours(hoursworked, overtimehours, n);
	findovertimepay(overtimehours, hourlyrate, overtimepay, n);
	findregularhours(hoursworked, regularhours, n);
	findregularpay(regularhours, regularpay, hourlyrate, n);
	findgrosspay(regularpay, overtimepay, grosspay, n);
	findtaxrate(grosspay, taxrate, n);
	findtaxamount(grosspay, taxamount, taxrate, n);
	findnetpay(grosspay, netpay, taxamount, n);
	printalldata(id, hoursworked, hourlyrate, overtimepay,
		grosspay, taxamount, netpay, n);
	return 0;
}//MAIN
//fUNCTION DEFINITIONS
int readalldata(long int id[], int hoursworked[], float hourlyrate[], int n){
	ifstream fin("employee.txt");
	n = 0;

	while (fin >> id[n] >> fname[n] >> lname[n] >> hoursworked[n] >> hourlyrate[n]) 
    n++;

	fin.close();
	return n;
}//READALLDATA

void findovertimehours(int hoursworked[], int overtimehours[], int n){
	for (int i = 0; i<n; i++){
		if (hoursworked[i]>40) overtimehours[i] = hoursworked[i] - 40;
		else overtimehours[i] = 0;
	}//FOR
}//FINDOVERTIMEHOURS

void findovertimepay(int overtimehours[], float hourlyrate[],
	float overtimepay[], int n){
	for (int i = 0; i<n; i++){
		overtimepay[i] = overtimehours[i] * hourlyrate[i] * 1.5;
	}//FOR
}//FINDOVERTIMEPAY

void findregularhours(int hoursworked[], int regularhours[], int n){
	for (int i = 0; i<n; i++){
		if (hoursworked[i]>40) regularhours[i] = 40;
		else regularhours[i] = hoursworked[i];
	}//FOR
}//FINDREGULARHOURS

void findregularpay(int regularhours[], float regularpay[],
	float hourlyrate[], int n){
	for (int i = 0; i<n; i++){
		regularpay[i] = regularhours[i] * hourlyrate[i];
	}//FOR
}//FINDREGULARPAY
void findgrosspay(float regularpay[], float overtimepay[],
	float grosspay[], int n){
	for (int i = 0; i<n; i++){
		grosspay[i] = regularpay[i] + overtimepay[i];
	}//FOR
}//FINDGROSSPAY

void findtaxrate(float grosspay[], float taxrate[], int n){
	for (int i = 0; i<n; i++){
		if (grosspay[i]>4000.00) taxrate[i] = 0.40;
		else if (grosspay[i]>3000.00) taxrate[i] = 0.30;
		else if (grosspay[i]>1000.00) taxrate[i] = 0.20;
		else taxrate[i] = 0.10;
	}//FOR
}//FINDTAXRATE

void findtaxamount(float grosspay[], float taxamount[],
	float taxrate[], int n){
	for (int i = 0; i<n; i++){
		taxamount[i] = grosspay[i] * taxrate[i];
	}//FOR
}//FINDTAXAMOUNT

void findnetpay(float grosspay[], float netpay[], float taxamount[], int n){
	for (int i = 0; i<n; i++){
		netpay[i] = grosspay[i] - taxamount[i];
	}//FOR
}//FINDNETPAY
int i = 0;
void printalldata(long int id[], int hoursworked[], float hourlyrate[],
	float overtimepay[], float grosspay[], float taxamount[],
	float netpay[], int n){
	float totalNetPay = 0;
	cout << setw(62) << "PAYROLL INSTITUTE" << endl;
	cout << setw(55) << "106 EASY WAYS LANE" << endl;
	cout << setw(58) << "PLEASANTVILLE N.Y. 11068" << endl;
	cout << " " << endl << endl;
	cout << "======================================================= " << endl;
	cout << fixed << setprecision(2);
	cout << "EMP ID" << setw(12)
		<< "FIRST NAME" <<setw (12)
		<< "LAST NAME" << setw(7)
		<< "HOURS" << setw(6)
		<< "RATE" << setw(10)
		<< "OVERPAY" << setw(11)
		<< "GROSSPAY" << setw(8)
		<< "TAX" << setw(11)
		<< "NETPAY" << endl;

	for (i = 0; i<n; i++){
		totalNetPay += netpay[i];
		cout << "" << id[i] << setw(31)
			<< hoursworked[i] << setw(9)
			<< hourlyrate[i] << setw(7)
			<< overtimepay[i] << setw(12)
			<< grosspay[i] << setw(10)
			<< taxamount[i] << setw(10)
			<< netpay[i] << endl;
	}//FOR
	cout << "======================================================= " << endl;
	cout << "AVERAGE NETPAY FOR ALL EMPLOYEES IS:" << endl;
	cout << setw(18) << totalNetPay / i << endl;
	cout << endl;
	system("PAUSE");
}//PRINTALLDATA
//end source code
}

_____________________________________________________________________
In order to try and get it to work I also changed...(and tried combinations of changes with the following)

Line 8 to read
1
2
int readalldata(long int[], string[], int[], float[], const int);
}


line 17 to read
1
2
void printalldata(long int[], string [], int[], float[], float[], float[], float[], float[], int);
}


line 32 to read
1
2
n = readalldata(id, fname, lname, hoursworked, hourlyrate, MAXSIZE); //get all data
}


line 41 to read
1
2
3
printalldata(id, fname, lname, hoursworked, hourlyrate, overtimepay,
		grosspay, taxamount, netpay, n);
}


and line 46 to read
1
2
int readalldata(long int id[], string fname, string lname[], int hoursworked[], float hourlyrate[], int n){
}


I also added in statements to display the data.

I still can't get this to work and am very frustrated.
Last edited on
Line 6: Not needed as globals, since also declared at line 30. I realize this was probably a workaround to getting undefined references elsewhere.

As I pointed out previously, you need to add these arrays to your readalldata function at line 8 and 46:
 
int readalldata(long int id[], string fname[], string lname[], int hoursworked[], float hourlyrate[], int n)


And of course to the call at line 32:
 
n = readalldata(id, fname, lname, hoursworked, hourlyrate, MAXSIZE); //get all data 


You also need to add these new arrays to your printalldata functions at lines 17 and 113:
1
2
3
void printalldata(long int id[], string fname[], string lname[], int hoursworked[], float hourlyrate[],
	float overtimepay[], float grosspay[], float taxamount[],
	float netpay[], int n)

And again to the call at liine 41.
1
2
    printalldata(id, fname, lname, hoursworked, hourlyrate, overtimepay,
		grosspay, taxamount, netpay, n);








It works! Seriously I'm so happy right now. I've still got to modify this program and write two other programs off of it. But it works!

Thank You all so much I've been really stressed out about this and it works as intended and I'm one step closer to finishing the other two modified versions of it!
Topic archived. No new replies allowed.