Segment Fault.

So i`m trying to finish up a program i`ve been working on but I have hit a wall with a segment fault. It runs perfectly for a little bit but than as you go along you will hit a fault i will post my main.cc and my class. I`ll post any other files you guys would like to see just let me know.
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
main.c
#include <iostream>
#include <fstream>
#include <string>
#include "course.h"
#include "node.h"
#include "college.h"

using namespace std;

// This function displays the menu and returns the user's choice
int menu();

int main(){
    int choice;
    course c;
    string coursename;
    ifstream fin;
    ofstream fout;
    string username,filename, fullname;

        cout<<"Welcome to Your College Course Management.\n\n";
        cout<<"Begin by entering your username: ";
        getline(cin,username);
        filename = username + ".txt";
/* The next three lines have been moved down to only be called 
   if the program is being run the first time for this user
	cout<<"Now Enter Your Full name:";
	while(cin.peek()=='\n')cin.ignore();
        getline(cin,fullname);
*/
// The default constructor is called here
	College mycollege;
        fin.open(filename.c_str());
    if(!fin.fail()){
        mycollege.load(fin);
        fin.close();
	}
    else{
        cout<<"Now Enter Your Full name:";
	while(cin.peek()=='\n')cin.ignore();
        getline(cin,fullname);
        College mycollege(fullname);
	}
	choice = menu();
	while(choice != 0){
	    switch(choice){
	    case 1:
		cout<<"Enter a new course that you have taken.\n";
		cin>>c;
		mycollege.add(c);
		break;
	    case 2: 
		mycollege.display(cout);
		break;
	    case 3:
		cout<<"Enter the name/course number of the course you ";
		cout<<"need to remove.\n";
		cin>>coursename;
		mycollege.remove(coursename);
		break;
	    case 4:
		cout<<"Total hours = "<<mycollege.hours();
		break;
	    case 5:
		cout<<"Your current GPA = "<<mycollege.gpa();
		break;
	    case 6:{
		College acopy(mycollege);
		cout<<"Enter one a course you would like to remove if you could.\n";
		cin>>coursename;
	        acopy.remove(coursename);
		cout<<"This would make your GPA:"<<acopy.gpa();
   		cout<<"And your courselist would look like.";
		acopy.display(cout);
		cout<<"But your GPA is still really: "<<mycollege.gpa();
		break;
		} // the copy goes out of scope here - destructor runs
	    case 0:
		cout<<"Thank you for using course management.\n";
		break;
	    default:
		cout<<"Not an option.\n";
		break;
	    } //bottom of the switch
	    choice = menu();
	}// bottom or the while loop, which is a senteniel loop

    fout.open(filename.c_str());
    if(!fout.fail())
	mycollege.save(fout);
    else
	cout<<"Problem with saving data!\n";
    fout.close();

return 0;
}

int menu(){
    int choice;
    cout<<"Choose from the following options:\n";
    cout<<"1) Add an additional course to your record.\n";
    cout<<"2) Display all the courses taken thus far.\n";
    cout<<"3) Remove a course from your college record.\n";
    cout<<"4) Display the total hours thus far completed.\n";
    cout<<"5) Display your current GPA.\n";
    cout<<"6) Test your copy constructor.\n";
    cout<<"0) Quit - saving all changes.\n";
    cin>>choice;

    return choice;
}

class----------------------------------------------------------
#include "college.h"
using namespace std;

College::~College(){
   node* c = head_ptr;
   node* rm;
   while (c != NULL)
       rm = c;
       c = c->link();
       delete rm;
}
College::College(const College& source){
   if(source.head_ptr == NULL)
       head_ptr = NULL;
   else {
       head_ptr = new node(source.head_ptr->data());
       const node* sptr = source.head_ptr->link();
       node* dptr = head_ptr;
       while (sptr != NULL) {
           dptr->set_link(new node(sptr->data()));
           dptr = dptr->link();
           sptr = sptr->link();
       }
   }
}
void College::operator =(const College& source){
   if(source.head_ptr == NULL)
       head_ptr = NULL;
   else {
       head_ptr = new node(source.head_ptr->data());
       const node* sptr = source.head_ptr->link();
       node* dptr = head_ptr;
       while (sptr != NULL) {
           dptr->set_link(new node(sptr->data()));
           dptr = dptr->link();
           sptr = sptr->link();
       }
   }
}
//mutators
void College::add(course c) {
node* inserter = new node;
   inserter->set_data(c);
if (inserter->data() < head_ptr->data()) {
       node* tmp; //if new course is < head, make it new head
       tmp = head_ptr;
       head_ptr = inserter;
       head_ptr->set_link(tmp);
   } else {
       node* c; //walk through and add course node
       for (c = head_ptr; (c->link() != NULL) && (inserter->data() > c->link()->data()); c = c->link()){} //walk to node before insert point
       if (c->link() == NULL) {
           c->set_link(inserter); //if being set to last item
           return; //get out of function
       }
       inserter->set_link(c->link());
       c->set_link(inserter);
   }
}
void College::remove(const std::string& target){
   node* cursor = head_ptr;
   node* remove_ptr;
   if (cursor->data().get_course_number() == target){ //if removing head node
   remove_ptr = head_ptr;
   head_ptr = head_ptr->link();
   delete remove_ptr;
   cout << "target found at begining of list and removed!\n";
   return;
   }
   while (cursor->link() != NULL && cursor->link()->data().get_course_number() != target) //rm'd cursro ->link() check
   (cursor = cursor->link());
   if (cursor->link()->data().get_course_number() == target) {
   remove_ptr = cursor->link();
   cursor->set_link( cursor->link()->link() );
   delete remove_ptr;
   cout << "target found and removed!\n";
   return;       
   }
   cout << "target not found\n";
}
istream& College::load(istream& inp) {
   node* inserter;
   node* tail; //holds last node in list
   course data;
  
   while (inp && inp.peek() != EOF) {
       while (inp.peek()=='\n') inp.ignore();
       if(head_ptr == NULL) {
       data.input(inp); //start node list if first item
           head_ptr = new node;
           head_ptr->set_data(data);
           tail = head_ptr;
       } else {
       data.input(inp); //add to proper place on list
           inserter = new node;
           inserter->set_data(data);
           if ((head_ptr->link() == NULL) && (inserter->data() < head_ptr->data())){
               inserter->set_link(head_ptr); //if new 2nd node < head //make new node link point to head
               head_ptr = inserter; // turn head into the new first item

           } else if ((head_ptr->link() == NULL) && (inserter->data() > head_ptr->data())) {
           head_ptr->set_link(inserter);//if 2nd new node is larger than head
               tail = head_ptr->link();
           } else {
               if (inserter->data() < head_ptr->data()) {
                   node* tmp; //if new course is < head, make it new head
                   tmp = head_ptr;
                   head_ptr = inserter;
                   head_ptr->set_link(tmp);
               } else if (inserter->data() > tail->data()) {
                   tail->set_link(inserter); //add to end of list
                   tail = inserter;
               } else {
                   node* c; //walk through and add course node
                   for (c = head_ptr; inserter->data() > c->link()->data(); c = c->link()){} //walk to node before insert point
                   inserter->set_link(c->link());
                   c->set_link(inserter);
               }
           }
       }
   }
}
//accessors
void College::display(ostream& outp) const{
   for (node* j = head_ptr; j != NULL; j = j->link())
       j->data().output(outp);
}
int College::hours() const{
   int sum(0);
   for (node* j = head_ptr; j != NULL; j = j->link()){
       sum += j->data().get_hours();
   }
   return sum;
}
double College::gpa() const{
   double finalgpa(0);
   double sum(0);
   //double t_hrs(0);
   for (node* i = head_ptr; i != NULL; i = i->link()){
       sum += (i->data().get_number_grade() * i->data().get_hours());
       //t_hrs += i->get_hours();
   }
   finalgpa = (sum / hours());
   return finalgpa;
   // point value is number_grade * hrs per course
   // then sum points, divide by total hours
}
ostream& College::save(ostream& fout){
for (node* j = head_ptr; j != NULL; j = j->link())
       j->data().output(fout);
       return fout;
}
Is using the backwards apostrophe your special little thing? The main.c is pointless, too.
Unfortunately it’s my professors backwards apostrophe it messes with the couts quite a bit.
Topic archived. No new replies allowed.