Payroll Program using functions...no output

Hello,

I'm writing a payroll program using functions. I'm using an input file named "employee.txt" to input data into the program. The program function correctly (i.e. no compiler errors, perfect output) until I added the first and last names (as char variables). Then, all output ceased.

I don't want the exact answer. Rather, I'd like someone/anyone to just point me in the right direction. The program still doesn't have any compiler errors. So, I'm just stuck.

Thanks in advance for any advice.

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
#include<iostream>
#include<fstream>

using namespace std;

//function definitions
int readdata(char fname[], char lname[], long int empid[], int tothrworked[], float hrrate[], int n) {
	ifstream fin("employee.txt");
	n = 0;
	while (fin >> fname[n] >> lname[n] >> empid[n] >> tothrworked[n] >> hrrate[n]) n++;
	fin.close();
	return 0;
}//read data function

void calcothours(int tothrworked[], int othrworked[], int n) {
	for (int i = 0;i < n;i++) {
		if (tothrworked[i] > 40) othrworked[i] = tothrworked[i] - 40;
		else if (tothrworked[i] <= 40) othrworked[i] = 0;
	}//end of FOR
}//calculating overtime hours

void calcreghours(int reghrworked[], int tothrworked[], int n) {
	for (int i = 0;i < n;i++) {
		if (tothrworked[i] > 40) reghrworked[i] = 40;
		else if (tothrworked[i] <= 40) reghrworked[i] = tothrworked[i];
	}//end FOR
}//calculating regular hours

void calcotpay(int othrworked[], float hrrate[], float otpay[], int n) {
	for (int i = 0;i < n;i++) {
		otpay[i] = (hrrate[i] * 1.5) * othrworked[i];
	}//end of FOR
}//calculating overtime pay

void calcregpay(int reghrworked[], float hrrate[], float regpay[], int n) {
	for (int i = 0;i < n;i++) {
		regpay[i] = reghrworked[i] * hrrate[i];
	}//end FOR
}//calculating regular pay

void calcgrosspay(float regpay[], float otpay[], float grosspay[], int n) {
	for (int i = 0;i < n;i++) {
		grosspay[i] = regpay[i] + otpay[i];
	}//end FOR
}//calculating gross pay

void calctaxrate(float grosspay[], float taxrate[], int n) {
	for (int i = 0;i < n;i++) {
		if (grosspay[i] > 4000)taxrate[i] = 0.40;
		else if (grosspay[i] > 3000 && grosspay[i] <= 3999)taxrate[i] = 0.30;
		else if (grosspay[i] > 1000 && grosspay[i] <= 2999)taxrate[i] = 0.20;
		else taxrate[i] = 0.10;
	}//end FOR
}//calculating tax rate

void calctaxpaid(float grosspay[], float taxpaid[], float taxrate[], int n) {
	for (int i = 0;i < n;i++) {
		taxpaid[i] = grosspay[i] * taxrate[i];
	}//end FOR
}//calculating tax paid

void calcnetpay(float grosspay[], float netpay[], float taxpaid[], int n) {
	for (int i = 0;i < n;i++) {
		netpay[i] = grosspay[i] - taxpaid[i];
	}//end FOR
}//calculating net pay

void printdata(char fname[], char lname[], long int empid[], int tothrworked[], float hrrate[], float otpay[], float grosspay[], float taxpaid[], float netpay[], int n) {
	cout << "FIRST NAME" << "\t" << "LAST NAME" << "\t" << "EMP ID" << "\t" << "HOURS" << "\t" << "RATE" << "\t" << "OT PAY" << "\t\t" << "GROSSPAY" << "\t\t" << "TAX PAID" << "\t" << "NET PAY" << "\t" << endl; 
	for (int i = 0;i < n;i++) {
			cout << fname[i] << "\t" << lname[i] << "\t" << empid[i] << "\t" << tothrworked[i] << "\t" << hrrate[i] << "\t" << otpay[i] << "\t\t" << grosspay[i] << "\t\t\t" << taxpaid[i] << "\t\t" << netpay[i] << endl;
		}//end FOR
}//print all the data

//prototyping all functions
int readdata(char[], char[], long int[], int[], float[], const int);
void calcothours(int[], int[], int);
void calcreghours(int[], int[], int);
void calcotpay(int[], float[], float[], int);
void calcregpay(int[], float[], float[], int);
void calcgrosspay(float[], float[], float[], int);
void calctaxrate(float[], float[], int);
void calctaxpaid(float[], float[], float[], int);
void calcnetpay(float[], float[], float[], int);
void printdata(char[], char[], long int[], int[], float[], float[], float[], float[], float[], int);

int main() {
	const int MAXSIZE = 100;//maximum number of employees set at 100
	
	//declaration of local variables
	int n;
	char fname[MAXSIZE], lname[MAXSIZE];
	long int empid[MAXSIZE];
	int tothrworked[MAXSIZE], othrworked[MAXSIZE], reghrworked[MAXSIZE];//types of hours worked
	float hrrate[MAXSIZE];//pay rates
	float regpay[MAXSIZE], otpay[MAXSIZE], grosspay[MAXSIZE], netpay[MAXSIZE];//pay totals
	float taxrate[MAXSIZE], taxpaid[MAXSIZE];//taxes

	//calling all functions
	n = readdata(fname, lname, empid, tothrworked, hrrate, MAXSIZE);
	calcothours(tothrworked, othrworked, n);
	calcreghours(reghrworked, tothrworked, n);
	calcotpay(othrworked, hrrate, otpay, n);
	calcregpay(reghrworked, hrrate, regpay, n);
	calcgrosspay(regpay, otpay, grosspay, n);
	calctaxrate(grosspay, taxrate, n);
	calctaxpaid(grosspay, taxpaid, taxrate, n);
	calcnetpay(grosspay, netpay, taxpaid, n);
	printdata(fname, lname, empid, tothrworked, hrrate, otpay, grosspay, taxpaid, netpay, n);
	return 0;
}//end to MAIN 
Last edited on
closed account (48T7M4Gy)
Instead of trying to fathom 111 lines of code why not write and run a short test program with just fname, pass it to a function and see if you can print it out in the same loop. It is bad form to test irrelevant parts already working - that's what students do but it just wastes mountains of time. i.e. isolate the problem, solve it and incorporate the solution in the production code.

I suspect you have a problem with confusion over char, arrays of chars and the corresponding array[i] value in the passed value to the function. You might dump C-style strings and C++ <string>'s
I took your advice and wrote a test program. It still isn't working...even to produce a more basic output. I would imagine my error is somewhere in the way I handle the char variable. However, my treatment of the arrays seems sound. Can you elaborate on the issues you think I'm missing? And what does "dump" in this context mean? Abandon or something else? I imagine something else since I didn't use strings here...yet.

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
#include<iostream>
#include<fstream>

using namespace std;

//function definitions
int readdata(char fname[], char lname[], int n) {
	ifstream fin("employee.txt");
	n = 0;
	while (fin >> fname[n] >> lname[n]) n++;
	fin.close();
	return 0;
}//read data function

void printdata(char fname[], char lname[], int n) {
	cout << "FIRST NAME" << "\t" << "LAST NAME" << endl;
	for (int i = 0;i < n;i++) {
		cout << fname[n] << "\t" << lname[n] << endl;
	}//end FOR
}//print all the data

 //prototyping all functions
int readdata(char[], char[], const int);
void printdata(char[], char[], int);

int main() {
	const int MAXSIZE = 100;//maximum number of employees set at 100

	//declaration of local variables
	int n;
	char fname[MAXSIZE], lname[MAXSIZE];

	//calling all functions
	n = readdata(fname, lname, MAXSIZE);
	printdata(fname, lname, n);
	return 0;
}//end to MAIN 

Are you aware that your var char fname[] can hold only one name?
What is your input file ?
closed account (48T7M4Gy)
Can you elaborate on the issues you think I'm missing? And what does "dump" in this context mean?


1. Effectively you have an array of character arrays. That is what an array of c-style strings is. As Thomas mentioned you are currently only catering for one fname.

2. You are looking at a two dimensional array for each variable eg fname[max_length][max_no_of_names]. The maximum number of names obviously must be more that the number of names on the text file

3. You should be using the getline function with specified delimiters to read in the data from the textfile. eg see http://www.augustcouncil.com/~tgibson/tutorial/iotips.html for example.

Insofar as 'dump' is concerned for my money I would move completely away from c-style strings and use <string>'s to avoid getting bogged down.


BTW I might have missed it but you haven't opened the file.
http://www.cplusplus.com/doc/tutorial/files/ and adapting and example there this is how it's done with <string>'s

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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main () {
    string lname[10];
    string fname[10];
    int count = 0;
    
    
    ifstream myfile ("employee.txt");
    if (myfile.is_open())
    {
        while ( myfile >> lname[count] >> fname[count])
        {
            cout << lname[count] << ' ' << fname[count] << '\n';
            count++;
        }
        myfile.close();
    }
    
    else cout << "Unable to open file";
    
    return 0;
}


employee.txt:
Smith Bill
Jones Grace
Brown Mary

Topic archived. No new replies allowed.