1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <fstream>
#include <cmath>
#include <vector>
#include <string>
#include <cctype>
using namespace std;
struct record
{
int id;
string first;
string last;
double salary;
};
void openfile(ifstream &ins, ofstream &outs, string input, string output);
void load(vector<record> &allEmployees, ifstream &ins, ofstream &outs);
void print(ofstream &outs, vector<record> &allEmployees, int numemployees);
int main()
{
ifstream ins;
ofstream outs;
string input;
string output;
int numemployees = 0;
cout << "Enter the input name: ";
cin >> input;
cout << "Enter the output name: ";
cin >> output;
openfile(ins, outs, input, output);
vector<record> allEmployees;
load(allEmployees, ins, outs);
print(outs, allEmployees, numemployees);
return 0;
}
void openfile(ifstream &ins, ofstream &outs, string input, string output)
{
ins.open(input);
if (ins.fail())
{
cout << "Error: Attempted input file failed to open";
exit(0);
}
outs.open(output);
if (outs.fail())
{
cout << "Error: Attempted output file failed to open";
exit(0);
}
}
void load(vector<record> &allEmployees, ifstream &ins, ofstream &outs, int &numemployees)
{
int empid;
string firstn;
string lastn;
double empsalary;
int i = 0;
ins >> empid >> firstn >> lastn >> empsalary;
while (!ins.eof())
{
allEmployees.push_back(record());
allEmployees[i].id = empid;
allEmployees[i].first = firstn;
allEmployees[i].last = lastn;
allEmployees[i].salary = empsalary;
ins >> empid >> firstn >> lastn >> empsalary;
numemployees++;
i++;
}
}
void print(ofstream &outs, vector<record> &allEmployees, int numemployees)
{
outs << "ID"
<< " "
<< "Name"
<< " "
<< "Salary" << endl;
outs << "--------------------------------------------------------" << endl;
int i = 0;
double totalSal = 0;
do
{
outs << allEmployees[i].id << " " << allEmployees[i].first << " " << allEmployees[i].last << " $" << allEmployees[i].salary << endl;
totalSal += allEmployees[i].salary;
} while (i <= numemployees);
outs << "Total Salary: " << totalSal;
}
|