Payroll class assignment

Here is the assignment that I'm working on, I have my program below, but I don't know where I got wrong, the program don't produce the correct output. Can someone please tell me where I got wrong? Much appreciated!!

Class design:

Design a PayRoll class that has data members for an employee’s first and last name, hourly pay rate, number of hours worked. The default constructor will set the first and last name to empty string, and hours worked and pay rate to zero. The class must have mutator functions to set each of the data members of the class and accessors to access them. The class should also have the following member functions:

displayPayroll function that displays all data members of the a PayRoll instance on one line separated by blanks
readPayRoll function that reads data members of a PayRoll instance from an input stream associated with a text file (assume all data members are on one line separated by commas)
getGross function that will return a double calculated by multiplying the hours worked by the pay rate.
Driver program:
Write a program with an array of seven PayRoll objects. The program should read the name (first and last), pay rate and the number of hours for each employee and display each employee information followed by the employee’s gross pay, all on one line separated by blanks. Set the precision for printing the doubles to two decimal places.

Input Validation:

Do not accept values greater than 60 for the number of hours worked, simply have the set function set number of hours worked to 60, the maximum allowed.
Do not accept negative values for pay rate and hours worked (set them to 0)
Be sure to include an employee claiming to work more than 60 hours per week, negative pay rate, and negative hours worked.
Deliverables: Submit a text file of your source code files and screenshots showing your program meets problem specifications listed above.

And here's what I've got so far:
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
#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
#include<vector>

using namespace std;

class Payroll
{
private:
	double hours;
	double payrate;
	double gross;
	string first;
	string last;

public:
	Payroll()
	{
		hours = 0.0;
		payrate = 0.0;
		first = "";
		last = "";

	}

	Payroll(double h, double p, string f, string l)
	{
		hours = h;
		payrate = p;
		first = f;
		last = l;
	}

	void setHours(double h)
	{
		if (h >= 60||h<0) cout << "Hours should be a positive number less 60" << endl;
		else hours = h;
	}

	void setPayrate(double p)
	{
		if (p < 0) cout << "Pay rate cannot be negative" << endl;
		else payrate = p;
	}

	void setGross(double hr, double pr)
	{
		gross = hr * pr;
	}

	void setFirstname(string fname)
	{
		first = fname;
	}

	void setLastname(string lname)
	{
		last = lname;
	}

	double getHours()
	{return hours;}

	double getPayrate()
	{return payrate;}

	double getGross()
	{return gross;}

	string getFirstname()
	{return first;}

	string getLastname()
	{return last;}

	void readPayroll(ifstream &inFile)
	{
		string firstName, lastName;
		char comma;
		double hours, payRate;
		getline(inFile, firstName, ',');
		setFirstname(firstName);
		getline(inFile, lastName, ',');
		setLastname(lastName);
		inFile >> hours;
		setHours(hours);
		inFile >> comma;
		inFile >> payRate;
		setPayrate(payRate);
		setGross(hours, payRate);
		inFile.ignore(100, '\n');
	}

	void displayPayroll()
	{
		cout << fixed << setprecision(2);
		cout << getFirstname() << " ";
		cout << getLastname() << " ";
		cout << getHours() << " ";
		cout << getPayrate() << " ";
		cout << getGross() << " ";
	}


};


int main()
{		ifstream file("payroll.txt");
	if(!file)
	{
		cout << "Can't open file \"payroll.txt\"" << endl;
		return(EXIT_FAILURE);
	}
	vector<Payroll> payRolllist;
	Payroll temp;
	while (file)
	{
		temp.readPayroll(file);
		payRolllist.push_back(temp);
	}
	for (int i = 0; i < payRolllist.size(); i++)
		payRolllist[i].displayPayroll();


	system("pause");
	return 0;
}


Last edited on
Topic archived. No new replies allowed.