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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
#include <fstream>
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
class Employee
{
private:
int id; // Employee ID
string name; // Employee name
double hourlyPay; // Pay per hour
int numDeps; // Number of dependents
int type; // Employee type
public:
Employee (int initId = 0, string initName = "", double initHourlyPay = 0.0, int initNumDeps = 0, int initType = 0); // Constructor
void readContents(fstream &myfile);
bool set(int newId, string newName, double newHourlyPay, int newNumDeps, int newType);
// Employee class set functions
int getId()
{ return id; }
string getName()
{ return name; }
double getHourlyPay()
{ return hourlyPay; }
int getNumDeps()
{ return numDeps; }
int getType()
{ return type; }
};
struct PayInfo
{
float hours, // Total hours worked
otHrs, // Overtime hours
tax, // Tax amount deducted from gross pay
gross, // Total pay before tax
net; // Total pay after tax
};
// Employee class constructor
Employee::Employee(int initId, string initName, double initHourlyPay, int initNumDeps, int initType)
{
bool status = set (initId, initName, initHourlyPay, initNumDeps, initType);
if (!status)
{
id = 0;
name = "";
hourlyPay = 0.0;
numDeps = 0;
type = 0;
}
}
// Employee class set function
bool Employee::set( int newId, string newName, double newHourlyPay, int newNumDeps, int newType )
{
bool status = false;
if (newId > 0 && newHourlyPay > 0 && newNumDeps >= 0 && newType >= 0 && newType <= 1)
{
status = true;
id = newId;
name = newName;
hourlyPay = newHourlyPay;
numDeps = newNumDeps;
type = newType;
}
return status;
}
void Employee::readContents(fstream &myfile)
{
myfile >> id;
getline(myfile, name, '#');
myfile >> hourlyPay >> numDeps >> type;
// Test if data is read
while (!myfile.fail())
{
cout << id << name << hourlyPay << numDeps << type;
}
}
int main()
{
const int NUM = 6; // Set number of employees
Employee emp[NUM]; // Array of class Employee
ifstream myfile; // Read data from file
float totalGross = 0.00, // Holds gross pay
totalNet = 0.00; // Holds net pay
const float taxRate = 0.15; // Constant tax rate
// Open data file
myfile.open("master9.txt");
if (!myfile)
{
cout << "Error opening data file" << endl;
}
for (int i = 0; i < NUM; i++)
{
Employee readContents(myfile);
}
myfile.close();
return 0;
}
|