0

I'm trying to make a university project. I am trying to store the ids of the user which are as u1,u2,u3.... in the temp triple pointer so that I can use them later but the problem is that for first time, i.e when i=0 then temp works fine and updates the user but when I pass temp to the ReadDataFromFile() function for the second time(i=1) it does not update and continues to posses garbage value in it.

class users
{
private:
ID = firstName = lastName = 0;
        LikedPages = new Page * [12];
        FriendList = new User * [20];
        for (int i = 0; i < 12; i++)
            LikedPages[i] = 0;
        for (int i = 0; i < 20; i++)
            FriendList[i] = 0;
        LikedPagesCounter = 0;
        AddedFriendsCounter = 0;
public:
void ReadDataFromFile(fstream& myData, char**& array, int index)
    {
        char buffer[80];
        myData >> buffer;
        ID = Helper::GetStringFromBuffer(buffer);
        myData >> buffer;
        firstName = Helper::GetStringFromBuffer(buffer);
        myData >> buffer;
        lastName = Helper::GetStringFromBuffer(buffer);
        *buffer = {};
        int i = 0, j = 0;
        while (1)
        {
            myData >> buffer;
            if (*buffer == '-')
            {
                array[i] = nullptr;
                break;
            }
            else
            {
                int k = 0;
                while (k < 2)
                {
                    array[index][j] = buffer[k];
                    j++;
                    k++;
                }
                array[index][j] = ' ';
                ++j;
                AddedFriendsCounter++;
            }
            array[index][j] = '\0';
            i++;
        }
    }
User* SearchUserByID(char*& buffer)
    {
        char* currentUserID;
        int count = 0;
        int j;
        for (int i = 0; i < 20; i++)
        {
            bool flag = true;
            currentUserID = users[i]->getUserID();
            for (j = 0; currentUserID[j] != '\0'; j++)
            {
                if (currentUserID[j] != buffer[j])
                {
                    flag = false;
                    break;
                }
            }
            if (flag)
            {
                count = j;
                while (count > 0)
                {
                    int k = 0;
                    for (k; buffer[k] != '\0'; k++)
                    {
                        buffer[k] = buffer[k + 1];
                    }
                    buffer[k - 1] = '\0';
                    count--;
                    if (buffer[0] == ' ')
                        ++count;
                }
                return users[i];
            }
        }
        return nullptr;
    }
void AssociateFriends(User** array)
    {
        for (int i = 0; i < AddedFriendsCounter; i++)
        {
            FriendList[i] = array[i];
        }
    }
};
class Facebook
{
private:
User**users;
public:
void LoadUsersFromFile(const char* myFile)
    {
        fstream myData;
        char*** temp = nullptr;
        User** tempAddedFriends = nullptr;
        myData.open(myFile, ios::in);
        if (myData)
        {
            int total;
            myData >> total;
            users = new User * [total];
            temp = new char**[total];
            tempAddedFriends = new User * [20];
            for (int i = 0; i < total; i++)
            {
                temp[i] = new char* [20];
                for (int j = 0; j < 20; j++)
                {
                    temp[i][j] = new char[20];
                }
            }
            for (int i = 0; i < total; i++)
            {
                users[i] = new User;
                users[i]->ReadDataFromFile(myData, temp[i], i);
}
            for (int i = 0; i < total; i++)
            {
                for (int j = 0, k = 0;; j++)
                {
                    tempAddedFriends[j] = SearchUserByID(temp[i][k]);
                    int length = Helper::GetLength(**temp);
                    if (!length)
                        break;
                }
                users[i]->AssociateFriends(tempAddedFriends);
            }
for (int i = 0; i < total; i++)
{
    for (int j = 0; j < 20; j++)
    {
        delete[] temp[i][j];
    }
    delete[] temp[i];
}
delete[] temp;

        }
        else
        {
            cout << "\nNO USERS INFORMATION EXISTS";
        }
        myData.close();
    }
};

I tried to different solutions, like sending temp(triple pointer directly), temp[i][j](sending single pointer) but nothing seems to work. Have wasted 2 days on it, would be glad if someone could come up with a solution. I dont want to use vectors or double pointer as it not the requirement of the project. Would be glad if someone could tell a fix for this rather than a new approach. Thanks!

  • Don't forget that a triple pointer, is a pointer on a double pointer which point to a pointer which point a data. So there must stay in valid memory 4 different data! C++ has objets to store things, also moreover new and delete must be avoided or even forgotten. – dalfaB Apr 15 '23 at 17:15

0 Answers0