'Hash table reading in from file

For my project I need to make a hash table the has stock information in it.I think I have most of what I need to do working, but I have an issue filling the hash table with information form a text file. When the file is empty my program starts up and displays the menu correctly, but when I put information into the text file it freezes forever until Putty kills the program. Also, when I try to add stocks it doesn't seem to take the information correctly, which may be the cause of it taking forever to read in from the file. I've tried messing around with a couple of things in the file but nothing has worked for me.

Edit: after adding some cout statements I've found that in the loop "while(!in.eof())" is infinite and never reaches the end of the file and I'm not sure why.

hashTable.cpp:

#include "hashtable.h"
#include <iostream>
#include <fstream>
#include<cstring>
using namespace std;

void hashTable::initializeTable()
{
        table = new node*[capacity];

        int i;
        for(i=0; i<capacity; i++)
                table[i] = NULL;
}

hashTable::hashTable() : capacity(DEFAULT_CAPACITY), size(0)
{
        initializeTable();
}

hashTable::hashTable(char * fileName) : capacity(DEFAULT_CAPACITY), size(0)
{
        ifstream in;
        data currData;
        char tickerSymbol[100];
        char name[100];
        float netVal;
        char date[100];
        float yToD;

        initializeTable();

        in.open(fileName);
        if(!in)
        {
                cerr << "fail to open " << fileName << " for input!" << endl;
                return;
        }

        in.get(tickerSymbol, 100, ';');
        while(!in.eof())
        {
                in.ignore(100, ';');
                in.get(name, 100, ';');
                in.ignore(100, ';');
                in.ignore(100, ';');
                in >> netVal;
                in.ignore(100, ';');
                in.get(date, 100, ';');
                in.ignore(100, ';');
                in >> yToD;
                in.ignore(100, '\n');

                currData.setTickerSymbol (tickerSymbol);
                currData.setName (name);
                currData.setNetValue(netVal);
                currData.setDate(date);
                currData.setYToD(yToD);

                insert(currData);

                in.get(tickerSymbol, 10, ';');
        }
        in.close ();
}

hashTable::hashTable(const hashTable& aTable):capacity(aTable.capacity), size(aTable.size)


{
        table = new node*[capacity];

        int i;
        for(i=0; i<capacity; i++)
        {
                if (aTable.table[i] == NULL)
                        table[i] = NULL;
                else
                {
                        table[i] = new node(aTable.table[i]->item);

                        node * srcNode = aTable.table[i]->next;
                        node * destNode = table[i];
                        while(srcNode)
                        {
                                destNode->next = new node(srcNode->item);
                                destNode = destNode->next;
                                srcNode = srcNode->next;
                        }
                        destNode->next = NULL;
                }
        }
        }

const hashTable& hashTable::operator= (const hashTable& aTable)
{
        if(this == &aTable)
                return *this;
        else
        {
                destroyTable();

                table = new node*[capacity];
                capacity = aTable.capacity;
                size = aTable.size;

                int i;
                for(i=0; i<capacity; i++)
                {
                        if (aTable.table[i] == NULL)
                                table[i] = NULL;
                        else
                        {
                                table[i] = new node(aTable.table[i]->item);

                                    node * srcNode = aTable.table[i]->next;
                                node * destNode = table[i];
                                while(srcNode)
                                {
                                        destNode->next = new node(srcNode->item);
                                        destNode = destNode->next;
                                        srcNode = srcNode->next;
                                }
                                destNode->next = NULL;
                        }
                }
                return *this;
        }
}

void hashTable::destroyTable ()
{
        int i;
        for(i=0; i<capacity; i++)
        {
                node * head = table[i];
                node * curr;
                while(head)
   {
                        curr = head->next;
                        head->next = NULL;
                        delete head;
                        head = curr;
                }
        }

        delete [] table;
}
hashTable::~hashTable()
{
        destroyTable();
}

void hashTable::insert (const data& aData)
{
        char key[100];
        aData.getTickerSymbol(key);
        int index = calculateIndex(key);

        node * newNode = new node(aData);

 newNode->next = table[index];
        table[index] = newNode;
        size++;
}

bool hashTable::remove (char * key)
{
        int index = calculateIndex(key);

        node * curr = table[index];
        node * prev = NULL;
        char id[100];
        while (curr)
        {
                curr->item.getTickerSymbol (id);
                if(strcmp(key, id) == 0)
                {
                        if(!prev)
                                table[index] = curr->next;
                        else
                                prev->next = curr->next;

                        curr->next = NULL;
   delete curr;
                        size--;
                        return true;
                }
                else
                {
                        prev = curr;
                        curr = curr->next;
                }
        }
        return false;
}

bool hashTable::retrieve (char * key, data & aData)const
{
        int index = calculateIndex(key);

        node * curr = table[index];
        char id[100];
        while (curr)
        {
                curr->item.getTickerSymbol (id);
                if(strcmp(key, id) == 0)
 {
                        aData = curr->item;
                        return true;
                }
                else
                        curr = curr->next;
        }

        return false;
}

void hashTable::display (void)const
{
        int i;
        node * curr;

        cout << "Data in the table: " << endl << endl;
        for(i=0; i<capacity; i++)
        {
                for(curr = table[i]; curr; curr = curr->next)
                        cout << '\t' << curr->item << endl;
        }
}
int hashTable::getSize (void) const
{
        return size;
}

void hashTable::writeOut(char * fileName)
{
        ofstream out;

        out.open(fileName);
        if(!out)
        {
                cerr << "fail to open " << fileName << " for output!" << endl;
                return;
        }

        int i;
        char tickerSymbol[100];
        char name[100];
        node * curr;
        for(i=0; i<capacity; i++)
        {
                for(curr = table[i]; curr; curr = curr->next)
 {
                        curr->item.getTickerSymbol (tickerSymbol);
                        curr->item.getName (name);
                        out << tickerSymbol << ';' << name << ';' << curr->item.getNetValue () << '\n';
                }
        }
        out.close ();
}

int hashTable::calculateIndex (char * key)const
{
        char * c = key;
        int total = 0;
        while(*c)
        {
                total += *c;
                c++;
        }

        return total%capacity;
}     

main.cpp

#include <stdlib.h>
//#include <crtdbg.h>


#include "hashtable.h"
#include <iostream>
using namespace std;

void displayMenu();
char getCommand();
void executeCmd(char command, hashTable& aTable);

void getStock(data & stock);
int getInt(char * prompt);
float getFloat(char * prompt);
void getString(char * prompt, char * input);

void display(const hashTable & aTable);

const int MAX_LEN = 100;

int main()
{
           char command = 'a';
        char fileName[] = "data.dat";
        hashTable stocks(fileName);

        displayMenu();
        command = getCommand();
        while(command != 'q')
        {
                executeCmd(command, stocks);
                displayMenu();
                command = getCommand();
        }

        stocks.writeOut (fileName);
        cout << "\nThank you for using CWMS!" << endl << endl;
        return 0;
}

void displayMenu()
{
        cout << "\nWelcome to CS Stock Management System! "<< endl;
        cout << "\ta> add a stock" << endl
                 << "\tr> remove a stock" << endl

       << "\ts> search for a stock" << endl
                 << "\tl> list all the stocks" << endl
                 << "\tq> quit the application" << endl << endl;
}

char getCommand()
{
        char cmd;
        cout << "Please enter your choice (a, r, s, l or q):";
        cin >> cmd;
        cin.ignore(100, '\n');
        return tolower(cmd);
}

void executeCmd(char command, hashTable& aTable)
{
        data stock;
        char key[MAX_LEN];

        switch(command)
        {
        case 'a': getStock(stock);
            aTable.insert (stock);
                          cout << endl << "the stock has been saved in the database. " << endl;
                break;
        case 'r': getString("\nPlease enter the ticker symbol of the stock you want to remove: ", key);
                      aTable.remove(key);
                          cout << endl << key << " has been removed from the database. " << endl;
                break;
        case 's': getString("\nPlease enter the ticker symbol of the stock you want to search: ", key);
                          aTable.retrieve (key, stock);
                          cout << endl << "Information about " << key << ": " << endl << '\t' << stock << endl;
                break;
        case 'l': display(aTable);
                break;
        default: cout << "illegal command!" << endl;
                break;
        }
}
void display(const hashTable & aTable)
{
        aTable.display();
}

void getStock(data & stock)
{
         char tickerSymbol[MAX_LEN];
         char name[MAX_LEN];
         float netVal;
         char date[MAX_LEN];
         float yToD;

         cout << "\nPlease enter information about the stock: " << endl;
         getString("\tticker symbol: ", tickerSymbol);
         getString("\tname: ", name);
         netVal = getFloat("\tnet asset value: ");
         getString("\tdate of that value: ", date);
         yToD = getFloat("\tyear to date return: ");

         stock.setTickerSymbol (tickerSymbol);
         stock.setName (name);
         stock.setNetValue (netVal);
         stock.setDate (date);
         stock.setYToD (yToD);
}
int getInt(char * prompt)
{
        int temp;
        cout << prompt;
        cin >> temp;
        while(!cin)
        {
                cin.clear ();
                cin.ignore(100, '\n');
                cout << "Illegal input -- try again: ";
                cin >> temp;
        }
        cin.ignore(100, '\n');
        return temp;
}
float getFloat(char * prompt)
{
        float temp;
        cout << prompt;
        cin >> temp;
        while(!cin)
        {
                cin.clear ();
                cin.ignore(100, '\n');
                cout << "Illegal input -- try again: ";
                cin >> temp;
        }
        cin.ignore(100, '\n');
        return temp;
}
void getString(char * prompt, char * input)
{
        cout << prompt;
        cin.get(input, MAX_LEN, '\n');
        cin.ignore (100, '\n');
}


Solution 1:[1]

Nevermind, I just didn't have semicolons at the end of the lines in my data.dat file so it was never reaching the end.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mason