Losing input stream?

I've been at this for a while. This section reads a text file into a structure array. If the array fills, double_size is called to increase the size. All this works up until the return from double_size and the next field is read in. It's as if the initialize function forgot about call_DB and/or the input stream. The error returned by MSVS is

"+ call_DB 0x0116fc2c {firstname=<Error reading characters of string.> lastname=<Error reading characters of string.> ...} call_record *"

I know this is the 'wrong' way to manage memory however this is an intro class...thanks in advance for any help!


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
void initialize(call_record *call_DB, int & count, int & size)
{
	ifstream in;
	in.open("callstats_data.txt");

	if (in.fail())
	{
		cout << "INPUT FILE DID NOT OPEN\n";
		exit(1);
	}
	
	count = 0;

	while(!in.eof())
	{
		if (is_full(count,size))
		{
			double_size(call_DB, count, size);  // INCREASING THE SIZE OF THE DYNAMIC ARRAY
		}
		if (in.fail())
		{
			cout << "INPUT FILE DID NOT OPEN\n";
			exit(1);
		}
		
		in >> call_DB[count].firstname;
		in >> call_DB[count].lastname;
		in >> call_DB[count].cell_number;
		in >> call_DB[count].relays;
		in	>> call_DB[count].call_length;
		
		count++;

	}
	
	in.close();


}



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
void double_size(call_record *call_DB, int & count, int & size)
{

	//step 1: double the capacity which is size in the program
	size *= 2;

	//step 2: allocate new memory

	call_record *temp = new call_record[size];

	//step 3: copy contents of old memory (call_DB) into new memory
	
	
	for (int i = 0; i < count; i++)
	{
		temp[i] = call_DB[i];
	}

    //step 4: de-allocate old memory pointed to by call_DB; 
	//        remember an array name is a pointer to the 1st element

	delete [] call_DB;

	//step 5: set call_DB to point to new memory;
	call_DB = temp;

    //step 6: all done!
	
}
Last edited on
Topic archived. No new replies allowed.