Homework Problem, Arrays of Payroll objects

The problem states that:
Design a PayRoll class that has data members for an employee’s hourly pay rate and number of hours worked. Write a program with an array of seven PayRoll objects. The program should read the number of hours each employee worked and their hourly pay rate from a file and call class functions to store this information in the appropriate objects. It should then call a class function, once for each object, to return the employee’s gross pay, so this information can be displayed.

My professor has also included that the user should be able to input the data (not from a data file) and that the user can choose to stop entering payroll objects before they reach 7.

What I cannot figure out is how to allow the user to stop entering payroll objects.

Here is my current code.

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>
using namespace std;


// Set Payroll class
class Payroll
{
	private:
		double hoursWorked;
		double payRate;
	public:
		Payroll();
		Payroll(double, double);
		void setHours(double);
		void setPayRate(double);
		double getHours();
		double getPayRate();
		double getGross();
};

// Define member functions
Payroll::Payroll()
{
	hoursWorked = 0;
	payRate = 0.0;
}
Payroll::Payroll(double h, double r)
{
	payRate = r;
	hoursWorked = h;
}
void Payroll::setHours(double h)
{
	hoursWorked = h;
}
void Payroll::setPayRate(double p)
{
	payRate = p;
}
double Payroll::getHours()
{
	return hoursWorked;
}
double Payroll::getPayRate()
{
	return payRate;
}
double Payroll::getGross()
{
	double gross = (hoursWorked) * payRate;
	return gross;
}


// Set the main program.
int main()
{
	// Set and declare variables
	const int NUM_EMPLOYEES = 7;
	Payroll employee[NUM_EMPLOYEES];
	double pay;
	double hours;
	int x;
	double grossPay;


	for (x = 0; x < NUM_EMPLOYEES; x++)
	{

		cout << "Enter Employee # " << (x) << " rate of pay per hour: ";
		cin >> pay;
		cout << "Enter Employee # " << (x) << " hours worked for the week: ";
		cin >> hours;

		employee[x].setPayRate(pay);
		employee[x].setHours(hours);
	}

	// Display the information to user.
	cout << "\n\nHere is the gross pay for each employee:" << endl;
	cout << fixed << showpoint << setprecision(2);

	for (int x = 0; x < NUM_EMPLOYEES; x++)
	{
		grossPay = employee[x].getGross();
		cout << "The gross pay for employee # " << (x) << " is: " << grossPay << endl;
	}
	system("pause");
	return 0;
}

This can be done with your current for loop like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//declare a char input
	for (x = 0; x < NUM_EMPLOYEES; x++)
	{

		cout << "Enter Employee # " << (x) << " rate of pay per hour: ";
		cin >> pay;
		cout << "Enter Employee # " << (x) << " hours worked for the week: ";
		cin >> hours;

		employee[x].setPayRate(pay);
		employee[x].setHours(hours);
//cout a statement like "Continue(y/n)?"
//cin input
//check if input is n, then break else do nothing
	}

Last edited on
Thank You! I followed what you said and seems to work for the purposes of the assignment.

Currently, I have it set up like this. Its works for what I want it to do, but should something be in the if statement?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
char choice;
for (x = 0; x < NUM_EMPLOYEES; x++)
	{

		cout << "Enter Employee # " << (x) << " rate of pay per hour: ";
		cin >> pay;
		cout << "Enter Employee # " << (x) << " hours worked for the week: ";
		cin >> hours;

		employee[x].setPayRate(pay);
		employee[x].setHours(hours);

		cout << "Continue? (Enter Y for yes/ Enter N for no)" << endl;
		cin >> choice;
		if (choice == 'Y' || choice == 'y')
		{

		}
		else
			break;
	}
Currently, I have it set up like this. Its works for what I want it to do, but should something be in the if statement?

So what is your exact problem? Can you give us more information?
Everything works fine. I meant if it was bad programming practice. I am still new to C++.
You only need to accept Y and N. Any other input will result in an error message.
Everything works fine. I meant if it was bad programming practice. I am still new to C++.

Looks good to me.
Topic archived. No new replies allowed.