Three employees in a company are up for a special pay increase. You are given a file, say Ch3_Ex6Data.txt, with the following data:
Miller Andrew 65789.87 5
Green Sheila 75892.56 6
Sethi Amit 74900.50 6.1
Each input line consists of employee’s last name, first name, current salary, and percent pay increase. For example, in the first input line, the last name of the employee is Miller, the first name is Andrew, the current salary is 65789.87, and pay increase is 5%. Write a program that reads data from the specified file and stores the output in the file CH3_Ex6Output.dat. For each employee, the data must be output in the following form: firstName lastName updatedSalary. Format the output of decimal numbers to two decimal places.
Write the Code:
/*
Project: Pay Increase
Description: This program reads data from a text file which contains
employee names and salaries. It calculates a pay increase and
stores the output in a new file.
*/
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
{
// Declare Variables
ifstream inData; // input variable for file
ofstream outData; // output variable for file
string lastName = "";
string firstName = "";
double salary = 0;
double raisePercentage = 0;
double newSalary = 0;
// Format precision to two decimal places
cout << fixed << setprecision(2);
outData << fixed << setprecision(2);
cout << "This program reads data from a text file which contains employee names ";
cout << "\nand salaries. It calculates a pay increase and stores the output in a new file.\n\n";
inData.open("Ch3_Ex6Data.txt"); // opens input file
outData.open("Ch3_Ex6Output.txt"); // creates and/or opens output file
// Process first employee
cout << "Reading data from old file...\n\n";
inData >> lastName >> firstName >> salary >> raisePercentage;
newSalary = salary * ((1 + raisePercentage) / 100) + salary;
cout << endl << firstName << " " << lastName << " has a new salary of $" << newSalary;
outData << firstName << " " << lastName << " " << newSalary;
// Process second employee
inData >> lastName >> firstName >> salary >> raisePercentage;
newSalary = salary * ((1 + raisePercentage) / 100) + salary;
cout << endl << firstName << " " << lastName << " has a new salary of $" << newSalary;
outData << firstName << " " << lastName << " " << newSalary;
// Process third employee
inData >> lastName >> firstName >> salary >> raisePercentage;
newSalary = salary * ((1 + raisePercentage) / 100) + salary;
cout << endl << firstName << " " << lastName << " has a new salary of $" << newSalary;
cout << "\n\nWriting data to new file..." << endl << endl;
outData << firstName << " " << lastName << " " << newSalary;
inData.close(); // closes input file
outData.close(); // closes output file
return 0;
}