Hash funciton

I wrote this code to for HW. I'm supposed to do a hash function and display information about the searched ship, first I have to get the data from a file and then manage it. Although the code has no build errors once I start debugging it suddenly crashes at line #28, which is in the constructor, when the 1st and only object in main is created. I get a message that says "Cannot open file: ../../../../../src/gcc-5.1.0/libgcc/unwind-sjlj.c" I can't start debugging so most likely functions do not do what they are intended to do, but the problem here is the file that cant be open for some reason. I have run this code in other machines with the exact same issue. I have never seen this "error" before and google either apparently :c. HELP.

shiprecords.txt file:
1009 1 "Royal Queen" 2015 160
1010 2 "Carnival" 2016 1600
1019 1 "Ocean King" 2013 110
1029 2 "Royal Prince" 2012 2000
1039 2 "Royal Princess" 2010 2100
1014 2 "Royal Caribbean" 2016 1600

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <iostream>
#include <fstream>
#include <string>


using namespace std;

struct ships
{
    int serialNum = NULL;
    string type = NULL; /// cargo or pasingers.
    string name = NULL;
    string year = NULL;
    string cap = NULL; ///capacity.
    ships *next = NULL;
};

class LinkedList
{
private:
    ships *List[10];
    ships *head;
public:
    LinkedList()
    {
        string line;
        ifstream file;
        ships *newNode = new ships;
        ships *p;

        file.open("shiprecords.txt");
        if(file.is_open())
        {
            while(!file.eof()) 
            {
                getline(file, line); /// reads a line from file until end of file.
                for(int k = 0; line.length() >= 3; k++)  ///takes the line and breaks it into variables.
                {
                    char y = NULL;
                    if(line[0] == ' ')
                    {
                        int j;
                        for(j = 1; j < line.length(); j++)
                        if(line[j] != ' ')
                            break;
                        line.erase(0, j);
                    }
                    if(line[0] == '"')
                    {
                        y = '"';
                        line.erase(0, 1);
                    }
                    else
                        y = ' ';
                    int i;
                    for (i = 0; i < line.length(); i++)
                        if (line[i] == y)
                            break;
                    switch(k) ///assigns the selected text from the line and assigns it to a variable.
                    {
                        case 0: newNode->serialNum = stoi(line.substr(0, i));
                                break;
                        case 1: newNode->type = line.substr(0, i);
                                break;
                        case 2: newNode->name = line.substr(0, i);
                                break;
                        case 3: newNode->year = line.substr(0, i);
                                break;
                        case 4: newNode->cap = line.substr(0, i);
                                break;
                    }
                    line = line.erase(0, i + 1);
                }
                if(head != NULL) ///Once the object has all values assigned it links them together to create a list.
                {
                    while(p->next != NULL)
                        p = p->next;
                    p->next =newNode;
                }
                else
                    head = newNode;
            }
        }
        else
            cout << "File not found.";
    }



    void Hash() ///takes object from list and assigns them to their respective index in the List[] array
    {
        ships *n = head;
        while( n->next != NULL)
        {
            if(List[n->serialNum % 10] == NULL)
            {
                List[n->serialNum % 10] = new ships;
                List[n->serialNum % 10] = n;
            }
            else
            {
                ships *prevNode = NULL;
                ships *p = List[n->serialNum % 10];
                while(p->next != NULL && p->serialNum < n->serialNum)
                {
                    prevNode = p;
                    p = p->next;
                }
                if(p == List[n->serialNum % 10] && p->serialNum > n->serialNum)
                {
                    n->next = p;
                    List[n->serialNum % 10] = n;
                }
                else if(!p->next && p->serialNum < n->serialNum)
                    p->next = n;
                else
                {
                    prevNode->next = n;
                    n->next = p;
                }
            }
        }
    }

    void DisplayOne(int val)
    {
        ships *p = List[val % 10];
        while(p->next != NULL && p->serialNum != val)
            p = p->next;
        if(p->serialNum == val)
        {
            cout << "Serial Number: " << p->serialNum << endl;
            cout << "Type: " << p->type <<  endl;
            cout << "Name: " << p->name <<  endl;
            cout << "Year Built: " << p->year <<  endl;
            cout << "Capacity: " << p->cap <<  endl;
        }
        else
            cout << "Ship not found." << endl;

    }
    ~LinkedList()
    {
           ///delete dynamic memory.
    }

};

int main()
{
    int value = 1009;
    string line;
    ifstream inp;

        LinkedList record;
        while(!inp.eof())
            record.Hash();
        record.DisplayOne(value);
}
Last edited on
This is not correct. std::string cannot be null.
1
2
3
4
    string type = NULL;
    string name = NULL;
    string year = NULL;
    string cap = NULL;

If you want the strings to be empty you don't need to do anything. It's the default.
1
2
3
4
    string type;
    string name;
    string year;
    string cap;

If you want to be explicit you can do
1
2
3
4
    string type = {};
    string name = {};
    string year = {};
    string cap = {};
but this is not necessary.

This is also not correct, but harmless. If it didn't work it would just have given you a compilation error.
 
int serialNum = NULL;


Remember that NULL is for pointers, and pointer-like objects. Also note that in modern C++ we often use nullptr instead of NULL because it has a few advantages.
Last edited on
Also note that in modern C++ we often use nullptr instead of NULL

Even in older C++ we usually used 0. I guess the old (void*)0 definition would cause problems! And if NULL is always defined as plain old 0 (as it should be in C++), then it really has no purpose.
But I guess nullptr is better.
NULL is always defined as plain old 0 (as it should be in C++)

NULL is allowed to be defined as nullptr since C++11 but I'm not sure if any implementation actually do so.
NULL is allowed to be defined as nullptr since C++11

That's interesting. This is how it looks in gcc:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/* A null pointer constant.  */
#if defined (_STDDEF_H) || defined (__need_NULL)
#undef NULL		/* in case <stdio.h> has defined it. */
#ifdef __GNUG__
#define NULL __null
#else   /* G++ */
#ifndef __cplusplus
#define NULL ((void *)0)
#else   /* C++ */
#define NULL 0
#endif  /* C++ */
#endif  /* G++ */
#endif	/* NULL not defined and <stddef.h> or need NULL.  */
#undef	__need_NULL 
Thanks, guys, not only for telling me what was the issue but also for explaining.
Topic archived. No new replies allowed.