Linked list, how do I put this in a class? compiles but crashes

I coded this without putting it in a class and planned to do it at the end, it has been giving me a little trouble. Its a pretty long code, so any tips would be fine. I attempted to put the struct in the private section of a Library class.

It says in my book that the struct for making linked list is generally declared above the class template and in the class spec file. So I shouldnt store it in the private section? It seems like it would be better there to protect the information in the struct?

When I tried putting it in the private section, I changed line 26 to Library::node *temp, when trying to compile it says that it can't access private struct. Can someone explain to me the best way to put a linked list in the class, or should I just put it above the class like the book suggest. Thanks

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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#include <algorithm>
#include <iostream>
#include <string>
#include <cctype>
#include <fstream>

using namespace std;

struct node
  {  string title; 
     string author;         
     string subject;     
	 node *nxt;
  };

node *start_ptr = NULL;
int option = 0;


void sortTitle()
{
	string titleOrder[50][3];
	int counter;
	int result = 0;
	string sort;
	node *temp;
	string tempArray[50];

    temp = start_ptr;
    cout << endl;
    if (temp == NULL)
		cout << "The list is empty!" << endl;
    else
	{ 
		// Load list of titles
		for (int i=0; temp != NULL; i++)
		{
			titleOrder[i][0] = temp->title;
			titleOrder[i][1] = temp->author;
			titleOrder[i][2] = temp->subject;
			temp = temp->nxt;
			counter = i + 1;
		}
	}
	//copy to tempArray
	for (int i=0; i < counter; i++)
	{
		tempArray[i] = titleOrder[i][0];
	}

	//tolower array for sort
	for (int i=0; i < counter; i++)
	{
		transform(titleOrder[i][0].begin(), titleOrder[i][0].end(), titleOrder[i][0].begin(),static_cast < int(*)(int) > (tolower));
	}

	//sort

	for (int i=0; i < counter; i++)
	{
		for (int ii=0; ii < counter-1; ii++)
		{
			result = titleOrder[ii][0].compare(titleOrder[ii+1][0]);
			if( result > 0 )
			{
				sort = titleOrder[ii][0];
				titleOrder[ii][0] = titleOrder[ii + 1][0];
				titleOrder[ii + 1][0] = sort;
				sort = tempArray[ii];
				tempArray[ii] = tempArray[ii + 1];
				tempArray[ii + 1] = sort;
				sort = titleOrder[ii][1];
				titleOrder[ii][1] = titleOrder[ii + 1][1];
				titleOrder[ii + 1][1] = sort;
				sort = titleOrder[ii][2];
				titleOrder[ii][2] = titleOrder[ii + 1][2];
				titleOrder[ii + 1][2] = sort;
			}
		}
	}
	for (int i=0; i < counter; i++)
	{
		titleOrder[i][0] = tempArray[i];
		cout << "Book #" << i+1 << endl;
		cout << "---------------------------------------" << endl;
		cout << "Title:   " << titleOrder[i][0] << endl;
		cout << "Author:  " << titleOrder[i][1] << endl;
		cout << "Subject: " << titleOrder[i][2] << endl << endl;
	}
}

void sortAuthor()
{
	string authorOrder[50][3];
	int counter;
	int result = 0;
	string sort;
	node *temp;
	string tempArray[50];

    temp = start_ptr;
    cout << endl;
    if (temp == NULL)
		cout << "The list is empty!" << endl;
    else
	{ 
		// Load list of titles
		for (int i=0; temp != NULL; i++)
		{
			authorOrder[i][0] = temp->author;
			authorOrder[i][1] = temp->title;
			authorOrder[i][2] = temp->subject;
			temp = temp->nxt;
			counter = i + 1;
		}
	}
	//copy to tempArray
	for (int i=0; i < counter; i++)
	{
		tempArray[i] = authorOrder[i][0];
	}

	//tolower array for sort
	for (int i=0; i < counter; i++)
	{
		transform(authorOrder[i][0].begin(), authorOrder[i][0].end(), authorOrder[i][0].begin(),static_cast < int(*)(int) > (tolower));
	}

	//sort

	for (int i=0; i < counter; i++)
	{
		for (int ii=0; ii < counter-1; ii++)
		{
			result = authorOrder[ii][0].compare(authorOrder[ii+1][0]);
			if( result > 0 )
			{
				sort = authorOrder[ii][0];
				authorOrder[ii][0] = authorOrder[ii + 1][0];
				authorOrder[ii + 1][0] = sort;
				sort = tempArray[ii];
				tempArray[ii] = tempArray[ii + 1];
				tempArray[ii + 1] = sort;
				sort = authorOrder[ii][1];
				authorOrder[ii][1] = authorOrder[ii + 1][1];
				authorOrder[ii + 1][1] = sort;
				sort = authorOrder[ii][2];
				authorOrder[ii][2] = authorOrder[ii + 1][2];
				authorOrder[ii + 1][2] = sort;
			}
		}
	}
	for (int i=0; i < counter; i++)
	{
		authorOrder[i][0] = tempArray[i];
		cout << "Book #" << i+1 << endl;
		cout << "---------------------------------------" << endl;
		cout << "Author:   " << authorOrder[i][0] << endl;
		cout << "Title:  " << authorOrder[i][1] << endl;
		cout << "Subject: " << authorOrder[i][2] << endl << endl;
	}
}

void sortSubject()
{
	string subjectOrder[50][3];
	int counter;
	int result = 0;
	string sort;
	node *temp;
	string tempArray[50];

    temp = start_ptr;
    cout << endl;
    if (temp == NULL)
		cout << "The list is empty!" << endl;
    else
	{ 
		// Load list of titles
		for (int i=0; temp != NULL; i++)
		{
			subjectOrder[i][0] = temp->subject;
			subjectOrder[i][1] = temp->title;
			subjectOrder[i][2] = temp->author;
			temp = temp->nxt;
			counter = i + 1;
		}
	}
	//copy to tempArray
	for (int i=0; i < counter; i++)
	{
		tempArray[i] = subjectOrder[i][0];
	}

	//tolower array for sort
	for (int i=0; i < counter; i++)
	{
		transform(subjectOrder[i][0].begin(), subjectOrder[i][0].end(), subjectOrder[i][0].begin(),static_cast < int(*)(int) > (tolower));
	}

	//sort

	for (int i=0; i < counter; i++)
	{
		for (int ii=0; ii < counter-1; ii++)
		{
			result = subjectOrder[ii][0].compare(subjectOrder[ii+1][0]);
			if( result > 0 )
			{
				sort = subjectOrder[ii][0];
				subjectOrder[ii][0] = subjectOrder[ii + 1][0];
				subjectOrder[ii + 1][0] = sort;
				sort = tempArray[ii];
				tempArray[ii] = tempArray[ii + 1];
				tempArray[ii + 1] = sort;
				sort = subjectOrder[ii][1];
				subjectOrder[ii][1] = subjectOrder[ii + 1][1];
				subjectOrder[ii + 1][1] = sort;
				sort = subjectOrder[ii][2];
				subjectOrder[ii][2] = subjectOrder[ii + 1][2];
				subjectOrder[ii + 1][2] = sort;
			}
		}
	}
	for (int i=0; i < counter; i++)
	{
		subjectOrder[i][0] = tempArray[i];
		cout << "Book #" << i+1 << endl;
		cout << "---------------------------------------" << endl;
		cout << "Subject:   " << subjectOrder[i][0] << endl;
		cout << "Title:  " << subjectOrder[i][1] << endl;
		cout << "Author: " << subjectOrder[i][2] << endl << endl;
	}
}
	
	 

void add_node_at_end()
  {  node *temp, *temp2;   // Temporary pointers

     // Reserve space for new node and fill it with data
     temp = new node;
	 cin.ignore();
     cout << "Please enter the title of the book: ";
	 getline(cin, temp->title);
     cout << "Please enter the author: ";
     getline(cin, temp->author);
     cout << "Please enter the subject: ";
     getline(cin, temp->subject);
     temp->nxt = NULL;

     // Set up link to this node
     if (start_ptr == NULL)
       { start_ptr = temp;
       }
     else
       { temp2 = start_ptr;
         // We know this is not NULL - list not empty!
         while (temp2->nxt != NULL)
           {  temp2 = temp2->nxt;
              // Move to next link in chain
           }
         temp2->nxt = temp;
       }
  }

void displayList()
{  
	int sortList = 0;
	node *temp;
     temp = start_ptr;
     cout << endl;
     if (temp == NULL)
       cout << "The list is empty!" << endl;
     else
	 {
		 cout << "Please select an option : " << endl;
		 cout << "0. Exit the list display." << endl;
	     cout << "1. Sort list by title." << endl;
	     cout << "2. Sort list by author." << endl;
	     cout << "3. Sort list by subject." << endl;
		 
		 cout << endl << " >> ";
		 cin >> sortList;
		 
		 switch (sortList)
		 {
		 case 1 : sortTitle(); break;
	     case 2 : sortAuthor(); break;
	     case 3 : sortSubject();
		 }
		 
	 cout << "End of list!" << endl;
	 }
  }
Last edited on
rest of code:

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
void deleteByTitle()
{
	string titleToDel;
	node *temp1, *temp2;
	temp1 = start_ptr;
	bool found = false;

	if (start_ptr == NULL)
		cout << "The list is empty!" << endl;
	else
	{
		cout << " Enter the name of the title you wish to remove: " << endl;
		cout << endl << " >> ";
		cin >> titleToDel;

		temp1 = start_ptr;
		if (temp1->title == titleToDel)
		{
			start_ptr = start_ptr->nxt;
			delete temp1;
			found = true;
		}
		else
		{
			while (temp1->nxt != NULL && !found)
            { 
				temp2 = temp1;
				temp1 = temp1->nxt;
					
				if (temp1->title == titleToDel)
				{
					temp1->nxt = temp2->nxt;
					delete temp1;
					found = true;
				}
			
			
			
			}
        }
		if (found == true)
		{
			cout << "The title was found and deleted from the list." << endl;
		}
		else
		{
			cout << "Sorry, the title was not found in the list." << endl;
		}
	}	
}

void inputFromFile()
{
	 node *temp, *temp2;
	 string title, author, subject;
     ifstream myfile ("books.txt");
  
  if (myfile.is_open())
  {
    while (! myfile.eof())
    {
	  temp = new node;
      getline (myfile,temp->title);
	  getline (myfile,temp->author);
	  getline (myfile,temp->subject);
	  myfile.ignore(1,'\n');
      cout << title << author << subject << endl;
	  temp->nxt = NULL;
	
	  // Set up link to this node
	  if (start_ptr == NULL)
	  { 
		  start_ptr = temp;
	  }
	  else
	  { 
		  temp2 = start_ptr;
          // We know this is not NULL - list not empty!
          while (temp2->nxt != NULL)
          {  
			  temp2 = temp2->nxt;
              // Move to next link in chain
          }
          temp2->nxt = temp;
      }
	}
  }
  myfile.close();
    
}

void outputToFile()
{
	ofstream myfile;
    myfile.open ("books.txt");
	node *temp;
	temp = start_ptr;

	if (start_ptr != NULL)
	{
		myfile << start_ptr->title << endl << start_ptr->author << endl << start_ptr->subject << endl << endl;
		
		while (temp->nxt != NULL)
		{
			temp = temp->nxt;
			myfile << temp->title << endl << temp->author << endl << temp->subject << endl <<endl;
		}
		
	}
    else cout << "Unable to open file"; 

  	cout << "Save complete." << endl;
	myfile.close();
}

	

void main()
  {  
	  start_ptr = NULL;
	  
	  inputFromFile();
	  
	  do
	  {
	  cout << endl;
	  cout << "Please select an option : " << endl;
	  cout << "0. Exit the program." << endl;
	  cout << "1. Add a book to the end of the list." << endl;
	  cout << "2. Delete a book by title." << endl;
	  cout << "3. Display list." << endl;
      
	  cout << endl << " >> ";
	  cin >> option;

	  switch (option)
	    {
	      case 1 : add_node_at_end(); break;
	      case 2 : deleteByTitle(); break;
	      case 3 : displayList(); 
	    }
	}
     while (option != 0);
	 outputToFile();
}
Here is the code after putting in a class...the class compiles but crashes on startup..gives, Im using VS08.

Unhandled exception at 0x00415baa in CS 310 - Project 2.exe: 0xC0000005: Access violation reading location 0xcccccd2c.

library.h
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
#include <algorithm>
#include <iostream>
#include <string>
#include <cctype>
#include <fstream>

using namespace std;

class Library
{
      public:
         Library(); //Creates an empty library without books and without authors.
      
		 void sortTitle();
		 void sortAuthor();
		 void sortSubject();
         void add_node_at_end();
		 void displayList();
		 void deleteByTitle();
		 void inputFromFile();
		 void outputToFile();
		 int option;
      
      private:
		 struct node
		 {  
			 string title; 
			 string author;         
			 string subject;     
			 node *nxt;
		 };
		  
		  node *start_ptr;
		  

          
      
};  


mainprogram.cpp
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
#include "library.h"


void main()
  {  
	  Library lib;
	  lib.inputFromFile();
	  
	  do
	  {
	  cout << endl;
	  cout << "Please select an option : " << endl;
	  cout << "0. Exit the program." << endl;
	  cout << "1. Add a book to the end of the list." << endl;
	  cout << "2. Delete a book by title." << endl;
	  cout << "3. Display list." << endl;
      
	  cout << endl << " >> ";
	  cin >> lib.option;

	  switch (lib.option)
	    {
	      case 1 : lib.add_node_at_end(); break;
	      case 2 : lib.deleteByTitle(); break;
	      case 3 : lib.displayList(); 
	    }
	}
     while (lib.option != 0);
	 lib.outputToFile();
}

library.cpp part 1
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#include "library.h"

Library::Library()
{
node *start_ptr = NULL;
int option = 0;
}


void Library::sortTitle()
{
	string titleOrder[50][3];
	int counter;
	int result = 0;
	string sort;
	node *temp;
	string tempArray[50];

    temp = start_ptr;
    cout << endl;
    if (temp == NULL)
		cout << "The list is empty!" << endl;
    else
	{ 
		// Load list of titles
		for (int i=0; temp != NULL; i++)
		{
			titleOrder[i][0] = temp->title;
			titleOrder[i][1] = temp->author;
			titleOrder[i][2] = temp->subject;
			temp = temp->nxt;
			counter = i + 1;
		}
	}
	//copy to tempArray
	for (int i=0; i < counter; i++)
	{
		tempArray[i] = titleOrder[i][0];
	}

	//tolower array for sort
	for (int i=0; i < counter; i++)
	{
		transform(titleOrder[i][0].begin(), titleOrder[i][0].end(), titleOrder[i][0].begin(),static_cast < int(*)(int) > (tolower));
	}

	//sort

	for (int i=0; i < counter; i++)
	{
		for (int ii=0; ii < counter-1; ii++)
		{
			result = titleOrder[ii][0].compare(titleOrder[ii+1][0]);
			if( result > 0 )
			{
				sort = titleOrder[ii][0];
				titleOrder[ii][0] = titleOrder[ii + 1][0];
				titleOrder[ii + 1][0] = sort;
				sort = tempArray[ii];
				tempArray[ii] = tempArray[ii + 1];
				tempArray[ii + 1] = sort;
				sort = titleOrder[ii][1];
				titleOrder[ii][1] = titleOrder[ii + 1][1];
				titleOrder[ii + 1][1] = sort;
				sort = titleOrder[ii][2];
				titleOrder[ii][2] = titleOrder[ii + 1][2];
				titleOrder[ii + 1][2] = sort;
			}
		}
	}
	for (int i=0; i < counter; i++)
	{
		titleOrder[i][0] = tempArray[i];
		cout << "Book #" << i+1 << endl;
		cout << "---------------------------------------" << endl;
		cout << "Title:   " << titleOrder[i][0] << endl;
		cout << "Author:  " << titleOrder[i][1] << endl;
		cout << "Subject: " << titleOrder[i][2] << endl << endl;
	}
}

void Library::sortAuthor()
{
	string authorOrder[50][3];
	int counter;
	int result = 0;
	string sort;
	node *temp;
	string tempArray[50];

    temp = start_ptr;
    cout << endl;
    if (temp == NULL)
		cout << "The list is empty!" << endl;
    else
	{ 
		// Load list of titles
		for (int i=0; temp != NULL; i++)
		{
			authorOrder[i][0] = temp->author;
			authorOrder[i][1] = temp->title;
			authorOrder[i][2] = temp->subject;
			temp = temp->nxt;
			counter = i + 1;
		}
	}
	//copy to tempArray
	for (int i=0; i < counter; i++)
	{
		tempArray[i] = authorOrder[i][0];
	}

	//tolower array for sort
	for (int i=0; i < counter; i++)
	{
		transform(authorOrder[i][0].begin(), authorOrder[i][0].end(), authorOrder[i][0].begin(),static_cast < int(*)(int) > (tolower));
	}

	//sort

	for (int i=0; i < counter; i++)
	{
		for (int ii=0; ii < counter-1; ii++)
		{
			result = authorOrder[ii][0].compare(authorOrder[ii+1][0]);
			if( result > 0 )
			{
				sort = authorOrder[ii][0];
				authorOrder[ii][0] = authorOrder[ii + 1][0];
				authorOrder[ii + 1][0] = sort;
				sort = tempArray[ii];
				tempArray[ii] = tempArray[ii + 1];
				tempArray[ii + 1] = sort;
				sort = authorOrder[ii][1];
				authorOrder[ii][1] = authorOrder[ii + 1][1];
				authorOrder[ii + 1][1] = sort;
				sort = authorOrder[ii][2];
				authorOrder[ii][2] = authorOrder[ii + 1][2];
				authorOrder[ii + 1][2] = sort;
			}
		}
	}
	for (int i=0; i < counter; i++)
	{
		authorOrder[i][0] = tempArray[i];
		cout << "Book #" << i+1 << endl;
		cout << "---------------------------------------" << endl;
		cout << "Author:   " << authorOrder[i][0] << endl;
		cout << "Title:  " << authorOrder[i][1] << endl;
		cout << "Subject: " << authorOrder[i][2] << endl << endl;
	}
}

void Library::sortSubject()
{
	string subjectOrder[50][3];
	int counter;
	int result = 0;
	string sort;
	node *temp;
	string tempArray[50];

    temp = start_ptr;
    cout << endl;
    if (temp == NULL)
		cout << "The list is empty!" << endl;
    else
	{ 
		// Load list of titles
		for (int i=0; temp != NULL; i++)
		{
			subjectOrder[i][0] = temp->subject;
			subjectOrder[i][1] = temp->title;
			subjectOrder[i][2] = temp->author;
			temp = temp->nxt;
			counter = i + 1;
		}
	}
	//copy to tempArray
	for (int i=0; i < counter; i++)
	{
		tempArray[i] = subjectOrder[i][0];
	}

	//tolower array for sort
	for (int i=0; i < counter; i++)
	{
		transform(subjectOrder[i][0].begin(), subjectOrder[i][0].end(), subjectOrder[i][0].begin(),static_cast < int(*)(int) > (tolower));
	}

	//sort

	for (int i=0; i < counter; i++)
	{
		for (int ii=0; ii < counter-1; ii++)
		{
			result = subjectOrder[ii][0].compare(subjectOrder[ii+1][0]);
			if( result > 0 )
			{
				sort = subjectOrder[ii][0];
				subjectOrder[ii][0] = subjectOrder[ii + 1][0];
				subjectOrder[ii + 1][0] = sort;
				sort = tempArray[ii];
				tempArray[ii] = tempArray[ii + 1];
				tempArray[ii + 1] = sort;
				sort = subjectOrder[ii][1];
				subjectOrder[ii][1] = subjectOrder[ii + 1][1];
				subjectOrder[ii + 1][1] = sort;
				sort = subjectOrder[ii][2];
				subjectOrder[ii][2] = subjectOrder[ii + 1][2];
				subjectOrder[ii + 1][2] = sort;
			}
		}
	}
	for (int i=0; i < counter; i++)
	{
		subjectOrder[i][0] = tempArray[i];
		cout << "Book #" << i+1 << endl;
		cout << "---------------------------------------" << endl;
		cout << "Subject:   " << subjectOrder[i][0] << endl;
		cout << "Title:  " << subjectOrder[i][1] << endl;
		cout << "Author: " << subjectOrder[i][2] << endl << endl;
	}
}
	
	 

void Library::add_node_at_end()
{
	ofstream outfile;
    outfile.open ("books.log");
	node *temp, *temp2;   // Temporary pointers
	int nodeNum = 1;

	// Reserve space for new node and fill it with data
    temp = new node;
	cin.ignore();
	cout << "Please enter the title of the book: ";
	getline(cin, temp->title);
	cout << "Please enter the author: ";
	getline(cin, temp->author);
	cout << "Please enter the subject: ";
	getline(cin, temp->subject);
	temp->nxt = NULL;

    // Set up link to this node
    if (start_ptr == NULL)
	{ 
		start_ptr = temp;
		nodeNum = 1;
    }
    else
	{ 
		temp2 = start_ptr;
		// We know this is not NULL - list not empty
		while (temp2->nxt != NULL)
		{  
			++nodeNum;
			temp2 = temp2->nxt;
			// Move to next link in chain
		}
		temp2->nxt = temp;
	}
	outfile << "Insertion into node: " << nodeNum << endl;
	outfile << "Book #" << nodeNum << endl;
	outfile << "=========================================" << endl;
	outfile << "Title: " << temp->title << endl;
	outfile << "Author: " << temp->author << endl;
	outfile << "Subject: " << temp->subject << endl << endl;
	outfile.close();
}

void Library::displayList()
{  
	int sortList = 0;
	node *temp;
     temp = start_ptr;
     cout << endl;
     if (temp == NULL)
       cout << "The list is empty!" << endl;
     else
	 {
		 cout << "Please select an option : " << endl;
		 cout << "0. Exit the list display." << endl;
	     cout << "1. Sort list by title." << endl;
	     cout << "2. Sort list by author." << endl;
	     cout << "3. Sort list by subject." << endl;
		 
		 cout << endl << " >> ";
		 cin >> sortList;
		 
		 switch (sortList)
		 {
		 case 1 : sortTitle(); break;
	     case 2 : sortAuthor(); break;
	     case 3 : sortSubject();
		 }
		 
	 cout << "End of list!" << endl;
	 }
  }
library.cpp part2
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
void Library::deleteByTitle()
{
	ofstream outfile;
    outfile.open ("books.log");
	string titleToDel;
	node *temp1, *temp2;
	temp1 = start_ptr;
	bool found = false;
	int nodeNum = 1;

	if (start_ptr == NULL)
		cout << "The list is empty!" << endl;
	else
	{
		cout << " Enter the name of the title you wish to remove: " << endl;
		cout << endl << " >> ";
		cin >> titleToDel;

		temp1 = start_ptr;
		if (temp1->title == titleToDel)
		{
			start_ptr = start_ptr->nxt;
			found = true;
			nodeNum = 1;
		}
		else
		{
			while (temp1->nxt != NULL && !found)
            { 
				temp2 = temp1;
				temp1 = temp1->nxt;
				++nodeNum;
					
				if (temp1->title == titleToDel)
				{
					temp1->nxt = temp2->nxt;
					found = true;
				}
			}
        }
		if (found == true)
		{
			cout << "The title was found and deleted from the list." << endl;
			outfile << "Deletion of node: " << nodeNum << endl;
			outfile << "Book #" << nodeNum << endl;
			outfile << "=========================================" << endl;
			outfile << "Title: " << temp1->title << endl;
			outfile << "Author: " << temp1->author << endl;
			outfile << "Subject: " << temp1->subject << endl;

			delete temp1;
		}
		else
		{
			cout << "Sorry, the title was not found in the list." << endl;
		}
		outfile.close();
	}	
}

void Library::inputFromFile()
{
	 node *temp, *temp2;
	 string title, author, subject;
     ifstream myfile ("books.txt");
  
  if (myfile.is_open())
  {
    while (! myfile.eof())
    {
	  temp = new node;
      getline (myfile,temp->title);
	  getline (myfile,temp->author);
	  getline (myfile,temp->subject);
      myfile.ignore(1,'\n');
	  while (myfile.peek()=='\n')
	  {
	  myfile.ignore(1,'\n');
	  }
	  cout << temp->title << temp->author << temp->subject << endl;
	  temp->nxt = NULL;
	
	  // Set up link to this node
	  if (start_ptr == NULL)
	  { 
		  start_ptr = temp;
	  }
	  else
	  { 
		  temp2 = start_ptr;
          // We know this is not NULL - list not empty!
          while (temp2->nxt != NULL)
          {  
			  temp2 = temp2->nxt;
              // Move to next link in chain
          }
          temp2->nxt = temp;
      }
	}
  }
  myfile.close();
    
}

void Library::outputToFile()
{
	ofstream myfile;
    myfile.open ("books.txt");
	node *temp;
	temp = start_ptr;

	if (start_ptr != NULL)
	{
		myfile << start_ptr->title << endl << start_ptr->author << endl << start_ptr->subject << endl << endl;
		
		while (temp->nxt != NULL)
		{
			temp = temp->nxt;
			myfile << temp->title << endl << temp->author << endl << temp->subject << endl << endl;
		}
		
	}
    else cout << "The list is empty." << endl;

  	cout << "Save complete." << endl;
	myfile.close();
}
The error takes me to line 394 in library.cpp

where:
while (temp2->nxt != NULL)
{
temp2 = temp2->nxt;
is...well I took out the function..and then the program starts..but it crashed when I tried to add a node...same type of crash..and the error took me to another line of code where I used

while (temp2->next != NULL)

what do you suppose is causing it.not sure if its teh while or the temp2...or what..
seems a few of the other crashes take me to:

xstring
int __CLR_OR_THIS_CALL compare(const _Myt& _Right) const
{ // compare [0, _Mysize) with _Right
return (compare(0, _Mysize, _Right._Myptr(), _Right.size()));

I tried commenting out some of the things making it crash..seems like every function has something in it to make it crash...I tried making my node, not private..still crashes.
Topic archived. No new replies allowed.