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!
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();
}
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!
}