Simple Payroll Program

Feb 14, 2012 at 12:18am
closed account (2NyT0pDG)
This is my program 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
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;


//**********//


int main() {

const int empId = 7; //Number of employees
	int workers[empId] = {5658846, 4520125, 7895122,
						  8777541, 8451277, 1302850,
						  7580489}; //Employee ID numbers
int hours[empId]; //Holds hours worked
double payRate[empId]; //Holds pay rates

//Input the hours worked and the hourly pay rate.
cout << "Please enter the hours worked by " << empId
	 << " employees and their\n"
	 << "hourly pay rates.\n";
for (int index = 0; index < empId; index++)
{
	cout << "Please enter the hours worked by employee number " << (index+1) << ": ";
	cin >> hours[index];
	cout << "Please enter the pay rate for employee number " << (index+1) << ": ";
	cin >> payRate[index];
}

cout << "This is the gross pay for each employee:\n";
cout << fixed << showpoint << setprecision(2);

for (int index = 0; index < empId; index++)
{
	double grossPay = hours[index] * payRate[index];
	cout << "Employee #" << (index + 1);
	cout << ": earned $" << grossPay << endl << endl;
}
return 0;
}


How do I get it to show the individual numbers in empId when requesting the hours worked and the hourly wage? Also, how would I make it display their ID numbers with their total wage earnings at the end? And how do I get it not to accept negative numbers for hours or a hourly wage less than $6.00?
Last edited on Feb 14, 2012 at 12:20am
Feb 14, 2012 at 12:39am
How do I get it to show the individual numbers in empId when requesting the hours worked and the hourly wage?

You just need to loop through each element in the "workers" array using workers[index].
1
2
3
4
5
6
7
8
 
for (int index = 0; index < empId; index++)
{
	cout << "Please enter the hours worked by employee number " << (index+1) << " (ID = " << workers[index] << ") : ";
	cin >> hours[index];
	cout << "Please enter the pay rate for employee number "<< (index+1) << " (ID = " << workers[index] << ") : ";
	cin >> payRate[index];
}


Also, how would I make it display their ID numbers with their total wage earnings at the end?

Same thing as above.

And how do I get it not to accept negative numbers for hours or a hourly wage less than $6.00?

You would need some sort of loop to repeat until the user enters the correct response.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
        do
	{
		cout << "Please enter the hours worked by employee number " << (index+1) << " (ID = " << workers[index] << ") : ";
		cin >> hours[index];

		if(hours[index] < 0)
		{
			cout << "Enter in a positive number" << endl; 
		}
	}
	while(hours[index] < 0); 
	
	do
	{
		cout << "Please enter the pay rate for employee number "<< (index+1) << " (ID = " << workers[index] << ") : ";
		cin >> payRate[index];

		if(payRate[index] < 6)
		{
			cout << "The pay rate must be >= 6" << endl; 
		}
	}
	while(hours[index] < 6);


(The loop could be combined into 1 if you wanted to)
Last edited on Feb 14, 2012 at 12:41am
Feb 14, 2012 at 1:04am
closed account (2NyT0pDG)
I think I managed to combine the loops just fine. Everything checked out okay. I appreciate your help.
Topic archived. No new replies allowed.