Im having really hard time writing an Employee Payroll program that uses functions to read data from file then send all data to another file. I managed to construct some pieces of the code, but I cant figure out how to put it together. Here's the description of whats needed to be done:
Write a program that generates an Employee Payroll. All output should go to payroll.out. The input for the program should be read from a file, payroll.data. Payroll.data should contain the following information for 5 employees:
id hours rate
1 5.0 15.75
2 52.5 17.50
3 40.0 14.00
4 50.0 14.00
5 5.25 12.25
The first column contains the number of the employee, the second column is the number of hours worked, and the third is the hourly pay rate.
Your program should perform the following tasks:
First call a function printPersonalInfo() which will not receive any parameters and will not return a value. This function will print your name and a description of the program to the output file, payroll.out.
For each employee you are to read in their id, hours, and pay rate from the input file, payroll.data and print them out to the output file.
Next you should call a function calculateEmployeeData() and pass in two parameters: hours and pay rate. Your function calculateEmployeeData() should compute and print to the output file the number of overtime hours worked by that employee, their overtime pay, and their total salary. An employee earns straight time for the first forty hours of work and double-time for any additional hours. For example if an employee worked 45 hours earning $10 and hour, then the employee's overtime hours would be 5, their overtime pay would be $100.00 and their total pay would be $500.00. The function should return the total salary.
Then, your main program should call a function, checkHours() passing in hours to the function. The function checkHours() prints out the following messages to the output file (You must use nested if-else statments (i.e. USE else if) :
If hours is less than 10 print a message saying that the employee has worked too little.
If hours is between 10 and 20, print a message saying the employee should work more than 20 hours a week.
If hours is between 20 and 40 it should print a message thanking the employee for putting in the time.
Otherwise (greater than 40 hours), it should print a very nice message letting the employee know that s/he has gone above and beyond what is required.
After there are no more employees to process, the program should print out to the output file the total number of hours and total salary for all employees combined.
I entered all the required data into infile "payroll.data" and it successfully reads from it and sends to outfile "payroll.out". However, after I send original info to file and then try to add my name and discription, as well as employee salaries etc.... (which must be separate from original data in outfile) it only mixes everything up, overrides it, or just shows results for the last employee in the for loop instead of all 5. I'm not even sure what to do in main. Please help me modify this code to work and put it together. Here's my code:
Expand|Select|Wrap|Line Numbers
- #include <iostream>
- #include <fstream>
- #include <math.h>
- using namespace std;
- void printPersonalInfo();
- double calculateEmployeeData(int hours, int payrate);
- void checkHours();
- double calculateEmployeeData(int hours, int payrate){
- int id;
- double totalsalary, extrahours = 0, overtimepay = 0, overtime = 0;
- double hours, payrate;
- ifstream infile;
- infile.open("payroll.data");
- ofstream outfile;
- outfile.open("payroll.out", ofstream::out);
- outfile.setf(ios::fixed,ios::floatfield);
- outfile.precision(2);
- for (int i = 1; i<= 5; i++){
- infile >> id;
- infile >> hours;
- infile >> payrate;
- outfile << id << " ";
- outfile << hours << " ";
- outfile << payrate << " \n\n";
- }
- for (int i = 1; i<= 5; i++){
- infile >> id;
- infile >> hours;
- infile >> payrate;
- if (hours > 40){
- extrahours = hours - 40;
- }
- if (hours > 40){
- overtimepay = payrate * 2;
- }
- if (hours > 40){
- overtime = extrahours * overtimepay;
- }
- totalsalary = (hours - extrahours) * payrate + overtime;
- outfile << "id: " << id << " ";
- outfile << "extrahours: " << extrahours << " ";
- outfile << "overtimepay: " << overtimepay << " ";
- outfile << "totalsalary: " << totalsalary << endl;
- }
- return totalsalary;
- }
- void printPersonalInfo(){
- cout<<"name"<<endl;
- cout<<"description"<<endl;
- }
- void checkHours(){
- if (hours <= 10)
- outfile << "you worked too little";
- else if (hours > 10 && hours <= 20)
- outfile << "Please work more than this";
- else if (hours > 20 && hours <= 40)
- outfile << "You wroked suficient amount of time";
- else
- outfile << "Thank you for working overtime";
- }
Any help is very much appreciated. Thank you in advance.