You have some data for each employee that you want to read from a stream an store in an array so I suggest that you create a class
for that data. The simplest class is a struct
(which by default gives public
access to all its members).
It could look like this:
struct employee {
int empID;
std::string lastName;
std::string firstName;
std::string social;
std::string payCode;
double hoursWorked;
};
You want to read from an open file to populate the member variables, so let's add a member function for that:
struct employee {
std::istream& read(std::istream& is) { // read from any istream, like an ifstream
is >> empID >> lastName >> firstName >> social >> payCode >> hoursWorked;
return is;
}
// the member variables ...
};
Now, if you have an employee
object, you can call the_object.read(timeSheet);
to read the data from the stream.
Similarly, you could add a member function to write the data to a stream:
struct employee {
std::ostream& write(std::ostream& os) const {
os << empID << ' ' << lastName << ' ' << firstName << ' '
<< social << ' ' << payCode << ' ' << hoursWorked << '\n';
return os;
}
// the rest of the members ...
};
With these simple functions, you can now read and write employees from/to streams and store them in some array-like object. Since the number of employees is unknown when you compile the program, I suggest storing the employees in a std::vector<employee>
.
Note, that you shouldn't use while (!timeSheet.eof())
(why is explained here). Instead, you can check the state of your file stream object after you've tried to extract something from the stream. A file stream returns !fail()
in boolean context which means that your loop should look like this:
while(timeSheet >> empID >> lastName >> firstName >> social >> payCode >> hoursWorked) {
// extraction succeeded
}
Since the read()
function we added above returns a reference to the stream it reads from, it can be used in the same way.
Full example:
#include <fstream>
#include <string>
#include <iostream>
#include <vector>
struct employee {
std::istream& read(std::istream& is) {
is >> empID >> lastName >> firstName >> social >> payCode >> hoursWorked;
return is;
}
std::ostream& write(std::ostream& os) const {
os << empID << ' ' << lastName << ' ' << firstName << ' '
<< social << ' ' << payCode << ' ' << hoursWorked << '\n';
return os;
}
int empID;
std::string lastName;
std::string firstName;
std::string social;
std::string payCode;
double hoursWorked;
};
int main() {
std::vector<employee> employees;
if(std::ifstream timeSheet("TimeSheet.txt"); timeSheet) {
employee temp;
while(temp.read(timeSheet)) { // only enter loop if extraction works
employees.push_back(temp); // store this employee in the vector
}
} else {
std::cout << "Could not open file.\n";
}
// print the employees
std::cout << "Employees:\n";
for(const employee& emp : employees) {
emp.write(std::cout);
}
/* an alternative way of printing all employees:
for(size_t i = 0; i < employees.size(); ++i) {
employees[i].write(std::cout);
}
*/
}
You can use a similar technique for the other files.