File input based program crashing

I'm witing a program in dev C++ that's supposed to open a sorted list of IP addresses and delete all the duplicates. However, when I try to run it I get the "this program has encountered an error and needs to close" dialog box. I believe that the program is unable to find and open the data file even though it is in the same folder as the program.

Client:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <fstream>   
#include <iomanip>
#include "linkedList.h"

using namespace std;

int main()
{
    cout << "test";  //program prints this line, then crashes
    Entry *n = BuildAddressBook();
    //PrintList(n);
    system("PAUSE");
}


Header:

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
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>

class Entry
{
      public:
          Entry* get_link() { return next; };
          std::string get_name() { return name; };
          void set_name(std::string & newname) { name = newname; };
          void set_link(Entry * new_link) { next = new_link; };

      
private:
          std::string name;
          Entry *next;

};
 Entry *BuildAddressBook();  //something in this function caused the crash
 Entry *GetNewEntry(std::string title);
 void PrintEntry(Entry *person);
 void Prepend(Entry *ent, Entry * & first);

 void DeallocateList(Entry *list);
 void PrintList(Entry *list);
 void InsertSorted(Entry * & list, Entry *newOne);
 void deleteDupe(Entry* &list);
 void list_remove(Entry* previous_ptr);

 bool operator==(Entry &lhs, Entry &rhs);


Implementation:

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
//Created by Julie Zelenkski, Stanford
//download original here: http://see.stanford.edu/materials/icspacs106b/H21-LinkedListCode.pdf
//Modified for std namespace by LaDawn Meade 2/16/09

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include "linkedList.h"

using namespace std;



Entry * GetNewEntry(string title)
{
    string name = title;
    if (name == "") return NULL;
    Entry *newOne = new Entry; // allocate in heap
    //newOne->link() = NULL; // no one follows
    newOne->set_link(NULL);
    return newOne;
}

void PrintEntry(Entry *person)
{
    cout << person->get_name() << endl;
}

void Prepend(Entry *ent, Entry * & first)
{
    //ent->link() = first;
    ent->set_link(first);
    first = ent;
}
void deleteDupe(Entry* &list)
{
     Entry *cur, *prev = NULL; // first node has no previous
     for (cur = list; cur != NULL; cur = cur->get_link())
       if (cur == prev)
       {
          
          list_remove(prev);
     
          cur = prev->get_link();
       }
       
}


Entry * BuildAddressBook() //crash occurs here, probably fails to open file
{
    ifstream pageRequests;
    pageRequests.open ("weblog.csc226.ipnsort", ios::in);  /*file located in saame
                                                           folder as program*/
    Entry *listHead = NULL;
    string currentLine;
    while (pageRequests.good()) {
        getline (pageRequests, currentLine);
        Entry *newOne = GetNewEntry(currentLine);
            //Prepend(newOne, listHead);
           // newOne->link() = listHead; // attach rest of list to node
        newOne->set_link(listHead);
        listHead = newOne; // node becomes head of list
    }
return listHead;
}


void DeallocateList(Entry *list)
{
    while (list != NULL) {
    Entry *next = list->get_link(); // save next ptr before deallocation
    delete list;
    list = next;
    }
}

void PrintList(Entry *list)
{
    for (Entry *cur = list; cur!= NULL; cur = cur->get_link())
    PrintEntry(cur);
}

void InsertSorted(Entry * & list, Entry *newOne)
{
    Entry *cur, *prev = NULL; // first node has no previous
    for (cur = list; cur != NULL; cur = cur->get_link()) {
    if (newOne->get_name() == cur->get_name()) return; // ignore dup
    if (newOne->get_name() < cur->get_name()) break;
    prev = cur;
    }
// now, "prev" is one before newEntry, "next" is after
    //newOne->link() = cur;
    newOne->set_link(cur);
    if (prev != NULL)
    //prev->link() = newOne;
        prev->set_link(newOne);
    else
    list = newOne; // note the special case!
}





void list_remove(Entry* previous_ptr)
{
     Entry *remove_ptr;
     
     remove_ptr = previous_ptr->get_link();
     previous_ptr->set_link(remove_ptr->get_link());
     delete remove_ptr;
}


bool operator==(Entry &lhs, Entry &rhs)
{
     return lhs.get_name() == rhs.get_name();
}

Topic archived. No new replies allowed.