Question: Need help fixing my add function. For some reason, it takes 2 inputs. college.cpp #include #include #include college.hpp #include course.hpp #include node.hpp using namespace std;
Need help fixing my add function. For some reason, it takes 2 inputs.
college.cpp
#include
#include
#include "college.hpp"
#include "course.hpp"
#include "node.hpp"
using namespace std;
College::~College(){
for(node* del = head; del != NULL; del = del->link()){
delete del;
}
head = NULL;
}
College::College(const College& o){
head = NULL;
node *new_head = o.head;
node *last = NULL;
while(new_head != NULL){
node *next = new node;
next->set_data(new_head->data());
next->set_link(NULL);
if(last != NULL){
last->set_link(next);
}
else{
head = new_head;
}
last = new_head;
new_head = new_head->link();
}
}
College College::operator =(const College& o){
if(this == &o){
return *this;
}
else{
delete head;
node *new_head = o.head;
node *last = NULL;
head = NULL;
while(new_head != NULL){
if(last == NULL){
head = new node;
head->set_data(new_head->data());
head->set_link(NULL);
last = head;
}
else{
last->set_link(new node);
last = last->link();
last->set_data(new_head->data());
last->set_link(NULL);
}
new_head = new_head->link();
}
return *this;
}
}
void College::add(course c){
cout << "Enter course information: " << endl;
cin >> c;
node* tmp;
if(head == NULL){
head = new node;
head->set_data(c);
head->set_link(NULL);
}
else{
for(tmp = head; tmp != NULL; tmp = tmp->link()){
tmp->set_link(new node);
tmp = tmp->link();
tmp->set_data(c);
tmp->set_link(NULL);
}
}
}
void College::remove(const std::string& n){
node* cursor = head;
node* rmptr;
if (cursor->data().get_course_number() == n){
rmptr = head;
head = head->link();
delete rmptr;
return;
}
while (cursor->link() != NULL && cursor->link()->data().get_course_number() != n){
(cursor = cursor->link());
}
if (cursor->link()->data().get_course_number() == n) {
rmptr = cursor->link();
cursor->set_link(cursor->link()->link());
delete rmptr;
return;
}
}
void College::display(ostream& ofs){
for(node* tmp = head; tmp != NULL; tmp = tmp->link()){
cout << tmp->data() << endl;
}
}
double College::hours()const{
course c;
int total_hours = 0;
for(node* tmp = head; tmp != NULL; tmp = tmp->link()){
total_hours += c.get_hours();
}
return total_hours;
}
double College::gpa()const{
course c;
double grade_pts = 0.0;
double GPA = 0.0;
for(node* tmp = head; tmp != NULL; tmp = tmp->link()){
grade_pts += c.get_number_grade() * c.get_hours();
}
GPA = (grade_pts / c.get_hours());
return GPA;
}
void College::load(istream& ins){
course tmp;
while(ins >> tmp){
add(tmp);
}
}
void College::save(ostream& outs){
for(node* tmp = head; tmp != NULL; tmp = tmp->link()){
outs << tmp->data() << endl;
}
}
college.hpp
#ifndef college_hpp
#define college_hpp
#include
#include
#include "course.hpp"
#include "node.hpp"
class College {
public:
typedef course value_type;
College() {name = "defaultname"; head = NULL;}
College(std::string n) {n = name; head = NULL;}
~College();
College(const College& o);
College operator =(const College& o);
void add(course c);
void remove(const std::string& n);
void display(std::ostream& ofs);
void load(std::istream& ins);
void save(std::ostream& outs);
void set_name(std::string n);
double hours()const;
double gpa()const;
private:
std::string name;
node *head;
};
#endif
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
