Read Data from File into Multiple Class Objects

I am very new to C++ and programming in general. Here is my questions (Thanks in advance for the help):

How do you create multiple new class objects using the default class constructor?

For a project I am having to write a program that writes three class objects into a file. That I have done... The next part is being able to read the data back into three separate class objects using a readData function and then displaying the data. I am completely lost at how to do this so I don't have any code in the readData function.

Here is an example of what the object looks like when it is being written to the file.

 
employee name(21, "first last", "45 East State", "661-9000", 30, 12.00);




Here is the bulk of my code the employee class is fairly basic but here is the default class constructor.

1
2
3
employee::employee ();

employee::employee(int locEmpNumber, string locName, string locaddress, string locphone, double locHrWorked, double locHrWage)


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
#include "employee.h"
#include <string>
#include <iomanip>
#include <iostream>
#include <fstream>
using namespace std;

void writeData (const employee& e);
void readData (const employee& e);
void printCheck (const employee& e);


int main( )
{
	//Declarations
	const int ONE = 1;
	const int TWO = 2;

	int userInput;
	
	cout << "This program has two options:" << endl;
	cout << "1 - Create a data files called 'EmployeeInfo.txt', or" << endl;
	cout << "2 - Read data from a file and print paychecks." << endl;
	cout << "Please enter (1) to create a file or (2) to print checks: ";

	cin >> userInput;

	if (userInput == ONE)
	{
		//Create employee objects:
		employee joe(37, "Joe Brown", "123 Main St.", "123-6788", 45, 10.00);
		employee sam(21, "Sam Jones", "45 East State", "661-9000", 30, 12.00);
		employee mary(15, "Mary Smith", "12 High Street", "401-8900", 40, 15.00);

		ofstream empFile ("EmployeeInfo.txt");

		//Employee objects to write themselves out to the file.
		writeData(joe);
		writeData(sam);
		writeData(mary);

		//Close the file.
		empFile.close();

		//Print an message that creation of the file is complete.
		system("CLS");
		cout << "\nCreation of 'EmployeeInfo.txt' has completed.\n";
		cout << "\nYou can now run option 2.\n";

		//Exit.
		system("PAUSE");
		return 0;
	}
	else if (userInput == TWO)
	{
		//Create three new Employee objects, using the default Employee constructor.

		//Open the file that you just saved.

		//Have each object read itself in from the file.

		//Call the printCheck( ) function for each of the three new objects, just as you did in the previous project.
	}
	else
	{
		system("CLS");		
		cout << "Incorrect entry.... Please try again and follow directions closely! \n" << endl;
		system("PAUSE");
		return 0;
	}


}

void writeData(const employee& e)
{
	fstream empFile;
	empFile.open ("EmployeeInfo.txt", ios::app);
	
	empFile << e.getEmpNumber() << "\n";
	empFile << e.getName() << "\n";
	empFile << e.getAddress() << "\n";
	empFile << e.getPhone() << "\n";
	empFile << e.getHrWorked() << "\n";
	empFile << e.getHrWage() << "\n";
}

void readData(const employee& e)
{
	fstream empFile;	
	empFile.open ("EmployeeInfo.txt", ios::in);

	if(empFile.fail())
	{
		cout << "File could not be open. Please try option 1 then option 2.\n" << endl; 
		return;
	}

}


Thanks again for the help.
hi, can you post the
employee.h
and
employee.cpp
files.
up until this level, i haven't tried class IO with files, so i want to experiment with your code, once i get a solution i'll point out a few points that might help you.
for the user choice, try enumerations:
http://www.cplusplus.com/doc/tutorial/other_data_types/
scroll down, they are in the end.
enumerations look more suitable in this situation.
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.
Topic archived. No new replies allowed.