In: Computer Science
C++
There is a file, called EmployeeInfo.txt, that contains information about company employees work for a week. You will write a program that reads the info from the file, and produces an output file called EmployeePay.txt with information about the employees pay amount for that week.
Details:
The input file is called EmployeeInfo.txt
There are 4 lines of input
Each line has the same form
Last Name, first Name, hours worked, pay rate, tax percentage,
extra deductions
Example: John Doe 36 17.75 21 5 means that John Doe worked 36
hours, gets paid $17.75 per hour, has 21% taken out as income tax,
plus an additional $5 taken out.
Your output file, called EmployeePay.txt, will have the form (for
each employee)
Last, First
Total Earned:
amountEarned
Income Tax:
taxAmount
Additional Deduction: extraDeduction
Pay Amount:
netIncome
Code:
#include<iostream> // header file for input output operations
#include<fstream> // header file for file operations
using namespace std;
int main(){ // main function
fstream file; // fstream object
string filename, word, lastname[100], firstname[100]; // variable to store data
int count = 0, line = 1; // line to keep count of line and array index(in array index each index belong to one person's info)
float hours[100], payrate[100],tax[100], extra[100], income_tax, extra_amount,total_earned,pay_amount;
filename = "EmployeeInfo.txt"; // filename
file.open(filename.c_str()); // opening file
while (file >> word) // reading words till end of the file
{
if(count == 0){ // if first word is read
lastname[line] = word;
}else if(count == 1){ // if second word is read
firstname[line] = word;
}else if( count == 2){ // if third word is read
hours[line] = stof(word);
}else if( count == 3){ // if fourth word is read
payrate[line] = stof(word);
}else if(count == 4){ // if fifth word is read
tax[line] = stof(word);
}else if( count == 5){ // if sixth word is read
extra[line] = stof(word);
}
count++; // incrementing the word count counter
if(count == 6){ // if one whlole line is read as there are just 6 word in each line
count = 0; // setting to count to zero indicated next worf will be first word
line++; // increasing the line number
}
}
ofstream my_file; // file object
my_file.open("EmployeePay.txt"); // opening file
for(int i=1;i<line;i++){
pay_amount = hours[i]*payrate[i]; // calculating the pay amount
income_tax = (tax[i]*pay_amount)/100; // calculating the income tax
extra_amount = (extra[i]*pay_amount)/100; // calculating the extra amount
total_earned = pay_amount - income_tax - extra_amount; // calculate total earned amount
my_file <<lastname[i]<<", "<<firstname[i]<<"\n"; // writing data to file
my_file << "Total Earned: " << total_earned << "\n"; // writing data to file
my_file << "Income Tax: " << income_tax << "\n"; // writing data to file
my_file << "Additional Deduction: " << extra_amount<< "\n"; // writing data to file
my_file << "Pay Amount: " << pay_amount<< "\n"; // writing data to file
}
my_file.close(); // closing the file
}
Coontent of EmployeeInfo.txt:
Content of EmployeePay.txt:
=> size of array is declared as 100 by asuming that there are at max 100 entries inthe EmployeeInfo.txt file. if the entries are more than 100 the change the size accoudingly in all the arrays.
Here array element with index 0 represent data of first person and index 1 represent data of second person and so on.
Note: Please let me know if you have any doubt or there is any change required in the code.