Three employees in a company are up for a special pay increase. You are given a
ID: 3788412 • Letter: T
Question
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 an 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 the 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.Explanation / Answer
Ch3_Ex6Data.txt.txt (Save the file under D Drive .So the path of the file pointing to it is D:\Ch3_Ex6Data.txt)
Miller Andrew 65789.87 5
Green Sheila 75892 6
Sethi Amit 74900.50 6.1
_____________________
C++ Program :
#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;
//Creating Employee Structure
struct Employee {
string firstname;
string lastname;
double currsal;
double increasePercent;
} emp;
int main() {
Employee emp[3];
//defines an input stream for the data file
ifstream dataIn;
//Defines an output stream for the data file
ofstream dataOut;
//Declaring variables
string lname,fname;
double csal,incPercent;
double updated_sal;
//Opening the input file
dataIn.open("D:\Ch3_Ex6Data.txt");
/* Getting the values from the input file and
* populate those values into Employee structure
*/
for(int i=0;i<3;i++)
{
//Getting the values from the input txt file
dataIn>>lname>>fname>>csal>>incPercent;
emp[i].lastname=lname;
emp[i].firstname=fname;
emp[i].currsal=csal;
emp[i].increasePercent=incPercent;
}
//Closing the intput file
dataIn.close();
//creating and Opening the output file
dataOut.open("D:\Ch3_Ex6Output.dat");
//Setting the precision
dataOut<<setprecision(2)<<fixed<<showpoint;
//Writing the data to the output file
for(int i=0;i<3;i++)
{
updated_sal=emp[i].currsal+(emp[i].currsal*emp[i].increasePercent);
//Writing the data to the output file
dataOut<<emp[i].firstname<<" "<<emp[i].lastname<<" "<<updated_sal<<endl;
}
//Closing the output file.
dataOut.close();
return 0;
}
______________________
We will get output file under D Drive.As we specified output file path as D:\Ch3_Ex6Output.dat
Ch3_Ex6Output.dat:
Andrew Miller 394739.22
Sheila Green 531244.00
Amit Sethi 531793.55
_____________________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.