Three employees in a company are up for a special pay increase. You are given a file, say Ch3_Ex5Data.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 a file specified by the user at runtime (i.e. your program should accept the filename as user input) and stores the output in the file Ch3_Ex5Output.dat. To test your program, use the Ch3_Ex5Data.txt file.
Your program will not pass all checks if it does not accept a filename as input from the user.
For each employee, the data must be output in the following form: firstName lastName updatedSalary.
#include<conio.h>
#include <iostream>
# include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include <cmath>
#include<dos.h>
#include <bits/stdc++.h>
using namespace std;
/*
Three employees in a company are up for a special pay increase. You are given a file, say Ch3_Ex5Data.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%.
Instructions
Write a program that reads data from a file specified by the user at runtime (i.e. your program should accept the filename as user input)
and stores the output in the file Ch3_Ex5Output.dat. To test your program, use the Ch3_Ex5Data.txt file.
Your program will not pass all checks if it does not accept a filename as input from the user.
For each employee, the data must be output in the following form: firstName lastName updatedSalary.
*/
//Read text file and split string into numbers
string S;
vector<string> k;
#define MAX_EMP 3
vector<string> splitString(string str, string delimiter)
{
vector <string> result;
size_t pos=0;
string token;
while((pos = str.find(delimiter))!= std::string::npos)
{
token = str.substr(0,pos);
result.push_back(token);
str.erase(0,pos+delimiter.length());
}
if(!str.empty()) result.push_back(str);
return(result);
}
int main()
{
char x;
int n=0,LineSize[MAX_EMP],i;
string Name1 = "H:\\Ch3_Ex5Data.txt";
string Name2 = "H:\\Ch3_Ex5Output.txt";
string str,z,LastName[MAX_EMP],FirstName[MAX_EMP];
float Salary[MAX_EMP], Incr[MAX_EMP];
// fstream outfile;
std::ifstream infile(Name1.c_str());
std::ofstream outfile(Name2.c_str());
// outfile.open(Name2.c_str());
if (!infile)
{
cout << "Unable to open file: "<<Name1;
exit(1); // terminate with error
}
else
{
cout<<"\n\nInput File Contents:\n";
while (std::getline(infile, str))
{
z = "";
if(str.size() > 0) z = z + str;
k = splitString(&z[0]," ");
LineSize[n] = k.size();
for(i=0;i<k.size();i++)
{
cout<<"\t"<<k[i];
if(i==0) LastName[n] = k[i];
if(i==1) FirstName[n] = k[i];
if(i==2) Salary[n] = atof(k[i].c_str());
if(i==3) Incr[n] = atof(k[i].c_str());
}
cout<<"\n";
n++;
}
infile.close();
}
cout<<"\n\tUpdated Information:\n";
for(n=0;n<MAX_EMP;n++)
{
Salary[n] = Salary[n] + (Salary[n]*Incr[n])/100;
outfile<<setw(10)<<FirstName[n]<<setw(10)<<LastName[n]<<"\t"<<Salary[n]<<endl;
cout<<setw(20)<<FirstName[n]<<"\t"<<LastName[n]<<"\t"<<Salary[n]<<endl;
}
outfile.close();
}
Comments
Leave a comment