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.