I'm coming up with this error:

Unhandled exception at 0x013c4218 in Hash_Table.exe: 0xC0000005: Access violation reading location 0x0000001c.

Here's my driver:

#pragma once
#include <cctype>        // Provides toupper
#include <cstdlib>       // Provides EXIT_SUCCESS
#include <cstring>       // Provides strchr
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>       // Operate on strings
#include "hTable1.h"

using namespace std;

// get total weight
// Precon: pass in preconstructed table, a string to parse and find weight, and the start of the string
// Postcon: calculates one line from the file and does the appropriate math for molecular weights
double getTWeight(hTable1& hashTable, string line, int i);

// reads the table
// Precon: pass in a preconstructed hash table by reference and a string 
// Postcon: reads in one line of code and parses it and puts it in the table
void readHTable(hTable1& hashTable, string line);

int main()
{
    string line;
    hTable1 hashTable;
    string source = "PeriodicTableElements.txt";
    ifstream file_input;
    file_input.open(source);
    if (file_input.fail())
    {
        cerr << "Could not open input file." << endl;
        system("PAUSE");
        exit(0);
    }
    while (file_input.peek() != EOF)
    {
        getline(file_input, line, '\n');
        readHTable(hashTable, line);
    }
    file_input.close();     // End of insertion of elements into the hash table

    //**********************************************************************//

    string source1 = "formulas.txt";
    ifstream file_input1;
    file_input1.open(source1);
    if (file_input1.fail())
    {
        cerr << "Could not open formula file." << endl;
        system("PAUSE");
        exit(0);
    }
    while (file_input1.peek() != EOF)
    {
        getline(file_input1, line, '\n');
        double totalWeight = getTWeight(hashTable, line, 0);
        cout << line << "=" << totalWeight << endl;
    }
    file_input1.close();
    cout << endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}

void readHTable(hTable1& hashTable, string line)
{
    double weight;
    int i = 0;
    while (line[i] != ' ')
        ++i;
    string element = line.substr(0, i);
    int elementNumber = element[0] - 0;
    int weightLength = line.size() - i;
    string weightString = line.substr(i, weightLength);
    istringstream convert(weightString);
    if (!(convert >> weight))
        weight = 0;
    hashTable.insert(element, weight, elementNumber);
}

double getTWeight(hTable1& hashTable, string line, int i)
{
    int j;
    int multiply;
    double tWeight = 0.0;
    double weight;
    while (line[i] != '\0')
    {
        j = i;
        if (line[i] == '(')
        {
            ++i;
            int k = i;
            while (line[k + 1] != ')')
                k++;
            string lineHelp = line.substr(i, k - i + 1);
            weight = getTWeight(hashTable, lineHelp, 0);
            i = (k + 2);
            if (line[i] == '\0')
                tWeight = (tWeight + (weight * 1));
        }
        else
        {
            while (islower(line[i + 1]))
                i++;
            int k = (i - (j + 1));
            string element = line.substr(j, k);
            double elementNumber = element[0] - 0;
            weight = hashTable.retrieve(elementNumber, element);
            ++i;
            if (!(isdigit(line[i])))
                tWeight = (tWeight + (weight * 1));
        }
        j = i;
        while (isdigit(line[i]))
            i++;
        int k = (i - j);
        string inputPasser = line.substr(j, k);
        istringstream convert(inputPasser);
        if (!(convert >> multiply))
            multiply = 0;
        tWeight = (tWeight + (weight*multiply));
    }
    return tWeight;
}

and here is my hTable1.cpp

#pragma once
#include "hTable1.h"
#include<iostream>
#include<string>

hTable1::hTable1()
{
    for (int i = 0; i<SIZE; i++)
        array[i] = new Node(-1, " ");
}

void hTable1::Delete(Node* n)
{
    if (n->next != NULL)
        Delete(n->next);
    delete n;
    return;
}

hTable1::~hTable1()
{
    for (int i = 0; i<SIZE; i++)
    {
        if (array[i]->next != NULL)
            Delete(array[i]->next);
        delete array[i];
    }
}

void hTable1::insert(string pElement, double pWeight, double key)
{
    int index = hash(key);
    if (array[index]->data == -1 || array[index]->data == -2)
    {
        array[index]->data = pWeight;
        array[index]->element = pElement;
    }
    else
    {
        insert(array[index]->next, pElement, pWeight);
    }
}

void hTable1::insert(Node*& n, string pElement, double pWeight)
{
    if (n == NULL)
        n = new Node(pWeight, pElement);
    else
        insert(n->next, pElement, pWeight);
}

double hTable1::retrieve(double d, string pElement)
{
    int index = hash(d);
    if (array[index]->element == pElement)
        return array[index]->data;
    else
        return retrieve(d, pElement, array[index]->next);
}

double hTable1::retrieve(double d, string pElement, Node* n)
{
    if (n->element == pElement)
        return n->data;
    return retrieve(d, pElement, n->next);
}

int hTable1::hash(double d)
{
    int a = (int)d%SIZE;
    return a;
}

My error comes in to play on line 63 of my hTable1.cpp. It could be possible that I'm trying to dereference a nullptr but I can't figure this is out. Been burning rubber on this one for a while. Advice/Help would be greatly appreciated.

Edited 3 Years Ago by natekelsey: Hopefully make the title more eye catching

Please post the contents of the htable1 header file.

Also, have you tried to build it debuggable and it run in a debugger? It should show you where it is corrupting memory, or trying to access invalid addresses, which is what your problem is, fundamentally.

P.S. This does't always work since building a block of code for debugging purposes often does stuff that will keep it from crashing where it did before, but it is always a step you should take.

Edited 3 Years Ago by rubberman

Here's my .h file

#pragma once
#include<cstdlib>
#include<string>

using namespace std;
struct Node
{
    double data;
    string element;
    Node* next;
    Node()
    {
        data = NULL;
        element = " ";
        next = NULL;
    }
    Node(double d, string ele)
    {
        data = d;
        element = ele;
        next = NULL;
    }
};

const int SIZE = 50;
class hTable1
{
    Node* array[SIZE];
public:
    // constructor
    hTable1(void);
    // destructor
    ~hTable1(void);
    // helps to delete dynamic allocated memory
    void Delete(Node* n);
    // inserts elemental data from the P-Table into a hash table
    void insert(string, double, double);
    // inserts elemental data from the P-Table into a hash table when there is a collision
    void insert(Node*& n, string element, double weight);
    // returns the value for the element
    double retrieve(double d, string element);
    // returns value for element when there is a collision
    double hTable1::retrieve(double d, string element, Node* n);
    // hash function to spread out the entries from the inserted data
    int hash(double);
    // overload the [] brackets
    hTable1 operator [] (int);
};

My debugger goes straight to here in the xstring.h

int compare(const _Myt& _Right) const _NOEXCEPT
        {   // compare [0, _Mysize) with _Right
        return (compare(0, this->_Mysize, _Right._Myptr(), _Right.size()));
        }
This article has been dead for over six months. Start a new discussion instead.