0
void BST::insert(node* tree, node* newnode)
{
    if (root == NULL)
    {
        root = new node;
        root->software = newnode->software;
        root->package_name = newnode->package_name;
        root->package_version = newnode->package_version;
        root->left = NULL;
        root->right = NULL;
        return;
    }
    if (tree->package_name == newnode->package_name)
    {
        return;
    }
    if (tree->package_name > newnode->package_name)
    {
        if (tree->left != NULL)
        {
            insert(tree->left, newnode);
        }
        else
        {
            tree->left->software = newnode->software;
            tree->left->package_name = newnode->package_name;
            tree->left->package_version = newnode->package_version;
            (tree->left)->left = NULL;
            (tree->left)->right = NULL;
            return;
        }
    }
    else
    {
        if (tree->right != NULL)
        {
            insert(tree->right, newnode);
        }
        else
        {
            tree->right->software = newnode->software;
            tree->right->package_name = newnode->package_name;
            tree->right->package_version = newnode->package_version;
            (tree->right)->left = NULL;
            (tree->right)->right = NULL;
            cout << "Node Added To Right" << endl;
            return;
        }
    }
}

void read(BST b,node* r)
{

    ifstream file_obj;

    file_obj.open("file_software.txt", ios::in);
    while (!file_obj.eof())
    {
        string name, version;
        int quantity, price;
        getline(file_obj, name, '$');
        file_obj >> version;
        file_obj >> quantity;
        file_obj >> price;

        package a(name, version, quantity, price);
        node* temp;
        temp = new node;

        temp->software = a;
        temp->package_name = name;
        temp->package_version = version;

        b.insert(r, temp);
    }
}


I am getting this error Exception thrown: read access violation. this was 0x40 error here if (tree->package_name == newnode->package_name) after inserting the first node as root. On this section of code something similar aswell if (tree->package_name > newnode->package_name) Exception thrown: read access violation. _Right was 0x40.

Seby
  • 1
  • 2
    You should check that `tree` and `newnode` are valid before dereferencing them. – cigien May 12 '20 at 22:53
  • OT: `while (!file_obj.eof())` [https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – drescherjm May 12 '20 at 22:55
  • 1
    Besides following best practices, checking for null pointers and so on, your best bet is to reproduce the problem with a debugger attached. You can then catch the access violation and inspect the variables, rather than eyeballing the code for problems. – castro May 12 '20 at 23:00

1 Answers1

0

In this line

tree->left->software = newnode->software;

of this part

        if (tree->left != NULL)
        {
            insert(tree->left, newnode);
        }
        else
        {
            tree->left->software = newnode->software;
            tree->left->package_name = newnode->package_name;
            tree->left->package_version = newnode->package_version;
            (tree->left)->left = NULL;
            (tree->left)->right = NULL;
            return;
        }

You are dereferencing what is guaranteed to be NULL thanks to the if statement.

It seems you forgot to allocate the node.

        if (tree->left != NULL)
        {
            insert(tree->left, newnode);
        }
        else
        {
            tree->left = new node; // add this
            tree->left->software = newnode->software;
            tree->left->package_name = newnode->package_name;
            tree->left->package_version = newnode->package_version;
            (tree->left)->left = NULL;
            (tree->left)->right = NULL;
            return;
        }

The same fix should be applied to tree->right.

Also, while (!file_obj.eof()) is wrong and you should check if the readings are successful instead.
(for more information: c++ - Why is iostream::eof inside a loop condition (i.e. while (!stream.eof())) considered wrong? - Stack Overflow)

One more point: the buffer assigned to temp in the function read isn't freed, so it will cause memory leak. It should be freed or statically allocated to avoid that.

MikeCAT
  • 61,086
  • 10
  • 41
  • 58
  • This is not the solution for `if (tree->package_name == newnode->package_name)` problem. It seems a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) should be posted for the issue. – MikeCAT May 12 '20 at 23:00