ok, there's sooo much to say...where shall i start...?
i respect your desire to not reveal your code to anyone.
the
employee.h and
employee.cpp look just fine, they don't contain bugs, i just didn't check
employee::calcPay(), you should check your maths on that.
now let me stick with the code posted above, keep with me step by step.
in line 35:
ofstream empFile ("EmployeeInfo.txt");
you don't really have to declare a stream here, as you're not using it, inside writeData() you declared another stream and that's the one you're actually using, that means you can delete line 43 as well :
empFile.close();
, but you shouldn't forget to close the stream inside
writeData().
sending the object to
writeData() isn't the same as making the object write itself, calling
writeData() actually makes it write the object on the file, if you want the object to actually write itself, you should define
writeData() as a member function.
in my opinion, this method of writing an object to a file is against the principles of OOP, see, the corner stone of OOP is (DATA CONTROLS CODE ACCESS TO IT), if you're going to let the code -in this case
writeData()- control the data stored inside employee that would be bad usage of OOP, you should instead define an inserter and an extractor that links between
employee and
streams, i mean operator overloading, or at least a member
writeData().
you must have an expectation about how each record should look like, i don't know what did you expect, but the records generated by this program look like this:
37
Joe Brown
123 Main St.
123-6788
45
10
21 |
if you actually need to read this exact record, you can use
fstream::getline(), other methods need some low level file IO with this record.
you can also use
operator>>() to extract data from the file, this is the simplest approach.
you need the employee file to stay opened until you finish reading it, otherwise you will lose the position you were at, that's why you should declare the stream inside the main function, and open the file in there, then you have to send both the stream and the object to
readData().
you can try this function:
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
|
void readData(ifstream& empFile,/*const*/ employee& e) //e cannot be const.
{
const int MAX_NAME_LENGTH = 40 , MAX_ADDRESS_LENGTH = 40;
int empNumber;
string name;
string address;
string phone;
double hrWorked;
double hrWage;
char NAME[MAX_NAME_LENGTH] = {'\0'};
char ADDRESS[MAX_ADDRESS_LENGTH] = {'\0'};
//read one employee record.
empFile>>empNumber; e.setEmpNumber(empNumber);
empFile.ignore(); //ignore the new line character.
empFile.getline(NAME,MAX_NAME_LENGTH,'\n');
name.assign(NAME);
e.setName(name);
empFile.ignore(); //ignore the new line character.
empFile.getline(ADDRESS,MAX_ADDRESS_LENGTH,'\n');
address.assign(ADDRESS);
e.setAddress(address);
empFile.ignore(); //ignore the new line character.
empFile.getline(PHONE,MAX_PHONE_LENGTH,'\n');
phone.assign(PHONE);
e.setPhone(phone);
empFile.ignore();
empFile>>hrWorked; e.setHrWorked(hrWorked);
empFile.ignore();
empFile>>hrWage; e.setHrWage(hrWage);
empFile.ignore();
}
|
anyway, this function is really lame, you should implement an overloaded version of an extractor (
operator>>), this can reduce the function size to half, and in the same time avoid defining so many extra strings and arrays and constants.....
another thing is in
employee.cpp, you don't have to define the arguments in the global scope, you should delete them all, each function arguments are enough for him to recognize his arguments, you don't have to define the arguments outside the function.
i think that answered your question, i probably should insert a link to operator overloading, i don't really have much time to search for that, i hope someone points you to a good article.
you should look for good articles your self, the only reason i solved this problem is because i saw your hard work trying to solve it.