Help with dynamic array of structures

Hi, I have got two issues I need help with.

First, I'm supposed to open an binary file, read the first piece of information (number of structures), and than dynamically allocate an array of structures, and read in the rest into the array of structure. It seems that I have achieved that, but when I try to print the structures (ex/ employee[0].firstname) anywhere besides where I read in the file, it does not print any information. It just prints blanks. I'm not quite sure what I am doing incorrectly, any tips?

Secondly I have the line commented out in the main function of deleting the allocated memory, because when I run the program it triggers a breakpoint, what am I doing incorrectly there, how do I properly delete that array of structures?

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
#include <iostream>
#include <iomanip>
#include <fstream>
using std::cout;
using std::ifstream;
using std::ios;
using std::setw;
using std::endl;
using std::left;

struct Emp //employee strcture given in the problem
{
	char firstname[20];
	char lastname[20];
	float wage;
	float hours;
}employee[15]; //create an array of employees for the structure

int ReadFile();
void PrintInfo(Emp * employee, int numRecords);

int main() {
	int numRecords = 0; //variable to store the number of records

	//call function to read in file
	numRecords = ReadFile();

	//call function to print info on screen
	PrintInfo(employee, numRecords);


	//delete[] employee;//deallocate memory

	system("pause");
	return 0;
}//end main

int ReadFile()
{
	int numRecords = 0;//variable to store number of records in file
	
	ifstream data("record.dat", ios::in | ios::binary); //open binary file
	if (data.is_open())//verify binary file is open
	{
		data.read(reinterpret_cast<char*>(&numRecords), sizeof(int)); //store number of records into variable
		Emp *employee;
		employee = new Emp[numRecords]; //create a dynamic array to accomodate data (from num of records variable)
																	 
		while (!data.eof()) 
		{
			for (int i = 0; i < numRecords; i++)//loop to read in stuctures
			{
				data.read ( reinterpret_cast<char*> (&employee[i]), sizeof(Emp));//read employees info in from binary file into dynamic array
			}//end for loop
		}
		data.close();//close binary file
	}//end of if open
	else
	{
		cout << "ERROR: Cannot open file.";//print out error if file is not open
	}

	cout << *employee[0].firstname;
	return numRecords; //return number of records
}// end ReadFile


void PrintInfo(Emp *employee, int numRecords)
{
	//header for screen
	cout << left << setw(30) << "Name" << setw(10) << "Wage" << setw(5) << "Hours Worked" << endl;
	//print employees info on screen
	for (int i = 0; i < numRecords; i++)//for loop to print records
	{
		cout << left << setw(30) << employee[i].firstname << " " << employee[i].lastname
			<< setw(10) << employee[i].wage << setw(5) << employee[i].hours << endl;

	}//end for loop to print info on screen

}//end PrintInfo 
While it is not the best, I have got around my issue. I was able to call my PrintInfo function from the ReadFile function. Reformatted some of the outputs also.

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
#include <iostream>
#include <iomanip>
#include <fstream>
using std::cout;
using std::ifstream;
using std::ios;
using std::setw;
using std::endl;
using std::left;

struct Emp //employee strcture given in the problem
{
	char firstname[20];
	char lastname[20];
	float wage;
	float hours;
}; //create an array of employees for the structure

void ReadFile();
void PrintInfo(Emp * employee, int numRecords);

int main() {
	int numRecords = 0; //variable to store the number of records

	//call function to read in file
	 ReadFile();

	system("pause");
	return 0;
}//end main

void ReadFile()
{
	int numRecords = 0;//variable to store number of records in file
	
	ifstream data("record.dat", ios::in | ios::binary); //open binary file
	if (data.is_open())//verify binary file is open
	{
		data.read(reinterpret_cast<char*>(&numRecords), sizeof(int)); //store number of records into variable
		Emp *employee;
		employee = new Emp[numRecords]; //create a dynamic array to accomodate data (from num of records variable)																	 
		while (!data.eof()) 
		{
			for (int i = 0; i < numRecords; i++)//loop to read in stuctures
			{
				data.read ( reinterpret_cast<char*> (&employee[i]), sizeof(Emp));//read employees info in from binary file into dynamic array
			}//end for loop
		}
		data.close();//close binary file
		//call function to print info on screen
		PrintInfo(employee, numRecords);
		delete[] employee;//deallocate memory
	}//end of if open
	else//print out error if file is not open
	{
		cout << "ERROR: Cannot open file.";
	}//end else
}// end ReadFile


void PrintInfo(Emp *employee, int numRecords)
{
	//header for screen
	cout << left << setw(20) << "First Name" << setw(20) << left << "Last Name" << left << setw(10) << left << "Wage" << left << setw(5) << "Hours Worked" << endl;
	//print employees info on screen
	for (int i = 0; i < numRecords; i++)//for loop to print records
	{
		cout << left << setw(20) << employee[i].firstname << setw(20)<< left << employee[i].lastname
			<< setw(15) << employee[i].wage << setw(10)  << employee[i].hours << endl;

	}//end for loop to print info on screen

}//end PrintInfo
Topic archived. No new replies allowed.