Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need help fixing my add function. For some reason, it takes 2 inputs. college.cp

ID: 3597227 • Letter: N

Question

Need help fixing my add function. For some reason, it takes 2 inputs.

college.cpp

#include <string>

#include <iostream>

#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 <iostream>

#include <string>

#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

Explanation / Answer

Hi friend, I can see that there in ONLy one input statement in add() function:

void College::add(course c){

// ONLY one input
  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);
}
}
}

Actually, add function is being called in "load" function where there are multiple inputs one after other.
So, in add function, after taking a input and adding successfully, it again comes to "load" function and it ask for input for "course"

void College::load(istream& ins){
course tmp;
  while(ins >> tmp){ // input in while loop
add(tmp);
}

}

Please let me know in case of any doubt.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote