Payroll Class with vectors
Nov 2, 2014 at 5:33am UTC
Hey guys I'm writing a Payroll class program to calculate gross pay
Write a program that processes Payroll objects. The program should read the
employee surname,number of hours worked,and their hourly pay-rate from a file
The file is below.
So the part im currently having trouble with is writing a function to read in the data from the file into a vector so i can manipulate the vector and display my end results. Any help on how i can fix this would be great thank you!
1 2 3 4 5 6 7 8
Jones 38.5 9.50
King 40.0 9.00
Lee 16.0 7.50
Sanchez 40.0 8.00
Smith 40.0 10.00
Taylor 38.0 8.00
Tucker 22.5 9.50
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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
class Payroll {
private :
double rate;
double hours;
string name;
double gross;
public :
Payroll();
void setRate(double );
void setHours(double );
void setGross(double );
double getRate() const {
return rate;
}
double getHours() const {
return hours;
}
double getGross() const {
return gross;
}
};
// Constructor
Payroll::Payroll ()
{
rate = 0;
hours = 0;
gross = 0;
}
// Vector read in function
void readFunction(ifstream &inFile, vector<Payroll> &list);
int main()
{
ifstream datafile;
datafile.open("payroll.txt" );
if (!datafile)
cout << "Error opening data file \n" ;
return 0;
}
void Payroll::setRate(double r)
{
if (r > 0) {
rate = r;
}
}
void Payroll::setHours(double h)
{
if (h > 0) {
hours = h;
}
}
void Payroll::setGross(double g)
{
gross = g;
g = hours * rate;
}
void readFunction(ifstream &inFile, vector<Payroll> &list)
{
string name;
double hours;
double rate;
double count; // count number of items in the file
if (inFile) {
double count; // count number of items in the file
// read the elements in the file into a vector
while ( inFile >> list ) {
list.push_back(count);
++count;
}
}
}
Nov 2, 2014 at 8:55am UTC
Seems like your problem is that you're trying to read from the file directly to the vector.
try something like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
void readFunction( ifstream &inFile, vector<Payroll> &list )
{
string name;
double hours;
double rate;
double count; // count number of items in the file
Payroll temp;
if ( inFile )
{
double count; // count number of items in the file
// read the elements in the file into a vector
while ( inFile >> temp )
{
list.push_back( temp );
++count;
}
}
}
You'll need to overload the >> operator of Payroll for this to work. Either that, or extract each data item into individual variables, and construct a temporary Payroll object from them. then push_back the temp object.
Last edited on Nov 2, 2014 at 8:57am UTC
Nov 2, 2014 at 9:00am UTC
still getting an error when i do that to...
Nov 2, 2014 at 9:06am UTC
did you see the edits to my above post?
I don't have time tonight to get into the details of how to make a constructor that would read an entire record. But someone else should be able to help.
Last edited on Nov 2, 2014 at 9:08am UTC
Nov 2, 2014 at 9:08am UTC
yea just saw it now. Thanks ill work on it and get back to you still a beginner so these things take me awhile.
Last edited on Nov 2, 2014 at 9:08am UTC
Nov 5, 2014 at 8:08pm UTC
ok here is my new code now I'm not really sure how i display the contents of my vector to see if i did my read function correctly
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 123 124 125
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
class Payroll {
private :
double rate;
double hours;
double gross;
string name;
public :
Payroll();
Payroll (string n, double h, double r);
void setRate(double );
void setHours(double );
void setName(string);
void setGross(double );
double getRate() const {
return rate;
}
double getHours() const {
return hours;
}
double getGross() const {
return gross;
}
};
// Constructor
Payroll::Payroll ()
{
name = " " ;
rate = 0;
hours = 0;
}
// parameter constructor
Payroll::Payroll (string n, double h, double r)
{
name = n;
hours = h;
rate = r;
}
// Vector read in function
void readFunction(ifstream &inFile, vector<Payroll> &list);
int main()
{
ifstream inFile;
Payroll payroll;
vector<Payroll> list;
inFile.open("payroll.txt" );
if (!inFile)
{
cout << "Error opening data file \n" ;
}
else
{
readFunction(inFile, list);
}
return 0;
}
void Payroll::setRate(double r)
{
if (r > 0) {
rate = r;
}
}
void Payroll::setHours(double h)
{
if (h > 0) {
hours = h;
}
}
void Payroll::setName(string n)
{
name = n;
}
void Payroll::setGross(double )
{
gross = hours * rate;
}
void readFunction( ifstream &inFile, vector<Payroll> &list )
{
string name;
double hours = 0;
double rate = 0;
double count = 0; // count number of items in the file
Payroll payroll;
if ( inFile )
{
// read the elements in the file into a vector
while ( inFile >> name >> hours >> rate )
{
payroll = Payroll(name, hours, rate);
list.push_back( payroll );
++count;
}
}
}
Last edited on Nov 5, 2014 at 8:54pm UTC
Topic archived. No new replies allowed.