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.
#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;
}