0

I am having some trouble on this college homework assignment. I've recently started programming again after a hiatus, and I'm not getting the values in the dynamically allocated pointer array I'm expecting. I'm checking them periodically with four loops. The output that begins to throw me off is at the end of the "readdata" function.

Right now when I print the array in my "resize" function I am getting the values I would expect, but when I print the "same" data ("Base" in resize and "ptrs" in readdata), I do not get the same results.

Also, one note. I did not create the function headers, those are supposed to be left alone.

Thank you in advance for the help!

#include <cassert>
#include <iostream>
#include <fstream>
#include <cctype>
using namespace std;

/*Type delcarations - NO MEMORY ALLOCATED (EVEN STATICALLY)*/
struct emprec   /*Employee record with two fields*/
{
    int id;
    float salary;
};
typedef emprec* emprecptr; /*Employee record pointer type*/
typedef emprecptr* indexarr; /* Pointer to a dynamic array of employee record pointers*/

int main()
{
    /* Rudimentary scope control in C/C++ */
    void readdata(indexarr&, int&);
    void printdata(indexarr, int);
    void copyindex(indexarr, indexarr&, int);
    void sortbyID(indexarr, int);
    void sortbySalary(indexarr, int);
    void deallocate(indexarr&, int&);
    indexarr DBindex, IDindex, Salaryindex;
    int dbsize = 10;
    char temp;

    emprecptr empArray;
    empArray = new emprec[dbsize];
    DBindex = &empArray;

    readdata(DBindex, dbsize);
    cout << "Original data:" << endl;
    cout << DBindex << endl;
    cout << (*DBindex)[0].id << endl;
    system("pause");
}

void resize(indexarr& Base, int OLDSIZE, int NEWSIZE)
/* Pre: Base is a (dynamic) indexarr of size OLDSIZE.
Post: Base is "EXPANDED" OR "SHRUNK" into a new (dynamic) indexarr of size
NEWSIZE, with data copied appropriately.
IMPORTANT NOTE: If being shrunk, it is assumed that there are only NEWSIZE valid
elements in the original array, and only that many are copied.*/
{
    // STUDENT IS TO FILL THIS IN.

    emprecptr newArray;
    newArray = new emprec[NEWSIZE];

    if (NEWSIZE < OLDSIZE)
    {
        for (int i = 0; i < NEWSIZE; i++)
        {
            newArray[i].id = (*Base)[i].id;
            cout << newArray[i].id << endl;
            newArray[i].salary = (*Base)[i].salary;
        }

        indexarr transfer = &newArray;
        
        Base = transfer;
        
    }
    
    for (int i = 0; i < NEWSIZE; i++)
        cout << (*Base)[i].id << endl;
    //delete[] newArray;
}


void readdata(indexarr& ptrs, int& size)
/*Pre: ptrs is a pointer that could point to a dynamic array of employee record pointers.
Post: ptrs points to a dynamic pointer array of the right size based on the
input read in from the data file named "p1data".
All the elements of the dynamic array are filled with pointers to dynamically
allocated employeed records, which in turn are filled with input data.*/
{
    void resize(indexarr&, int, int);

    // STUDENT IS TO FILL THIS IN

    ifstream myFile;
    myFile.open("p1data.txt");
    if (myFile.is_open())
    {
        int i = 0;
        int hold;
        while (!myFile.eof())
        {
            myFile >> (*ptrs)[i].id;
            myFile >> (*ptrs)[i].salary;
            i++;
        }
        i--; //To correct size because of the way C++ reads EOF

        myFile.close();
        resize(ptrs, size, i);
    }
    else
    {
        cout << "ERROR: The file could not be opened." << endl;
    }

    for (int i = 0; i < 5; i++)
        cout << ptrs[i]->id << endl;

};

Here is the text file that's being read.

10 20000

20 10000

30 40000

25 15000

15 45000
  • 1
    Recommended reading: [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – user4581301 Sep 14 '20 at 03:33
  • Rethink `typedef emprecptr* indexarr;` and how you are using it. Remember that [arrays decay to pointers](https://stackoverflow.com/questions/1461432/what-is-array-to-pointer-decay) Do you really want pointers to pointers? Make sure you understand what will happen when you increment a pointer to a pointer. – user4581301 Sep 14 '20 at 03:37
  • One take-away you should get from this is over aliasing can hide mistakes from you. – user4581301 Sep 14 '20 at 03:40

0 Answers0