I can\'t get my remove function to work in my college.cpp file. Currently gettin
ID: 3596962 • Letter: I
Question
I can't get my remove function to work in my college.cpp file. Currently getting this error:
Here is the full code:
collegemain.cpp
#include <iostream>
#include <fstream>
#include <string>
#include "course.hpp"
#include "node.hpp"
#include "college.hpp"
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. ";
cout<<"Begin by entering your username: ";
getline(cin,username);
filename = username + ".txt";
cout<<"Now Enter Your Full name:";
while(cin.peek()==' ')cin.ignore();
getline(cin,fullname);
// Here you are calling a constructor that takes a string as a
// parameter
College mycollege(fullname);
fin.open(filename.c_str());
if(!fin.fail())
mycollege.load(fin);
fin.close();
choice = menu();
while(choice != 0){
switch(choice){
case 1:
cout<<"Enter a new course that you have taken. ";
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. ";
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. ";
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. ";
break;
default:
cout<<"Not an option. ";
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! ";
fout.close();
return 0;
}
int menu(){
int choice;
cout<<"Choose from the following options: ";
cout<<"1) Add an additional course to your record. ";
cout<<"2) Display all the courses taken thus far. ";
cout<<"3) Remove a course from your college record. ";
cout<<"4) Display the total hours thus far completed. ";
cout<<"5) Display your current GPA. ";
cout<<"6) Test your copy constructor. ";
cout<<"0) Quit - saving all changes. ";
cin>>choice;
return choice;
}
course.cpp
#include "course.hpp"
#include<cstdlib>
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
course::course(){
hours = 0.0;
}
void course::input(std::istream& ins){
if(&ins == &cin){
cout<<"Course Number: ";
if(ins.peek() == ' ') ins.ignore();
getline(ins, course_number);
if(ins.eof()) return;
cout<<"Grade received: ";
getline(ins, grade);
grade[0] = toupper(grade[0]);
if(ins.eof()) return;
cout<<"Credit hours: ";
ins>>hours;
upper_course(); // makes all lower case letters into caps
}
else{
if(ins.peek() == ' ') ins.ignore();
getline(ins, course_number);
if(ins.eof()) return;
getline(ins, grade);
grade[0] = toupper(grade[0]);
if(ins.eof()) return;
ins>>hours;
}
}
void course::output(std::ostream& outs)const{
if(&outs == &cout){
outs<<"Course Number:"<<course_number<<endl;
outs<<"Grade received:"<<grade<<endl;
outs<<"Credit hours:"<<setprecision(2)<<hours<<endl;
}
else{
outs<<course_number<<endl;
outs<<grade<<endl;
outs<<setprecision(2)<<hours<<endl;
}
}
double course::get_number_grade()const{
if(grade == "A") return 4.0;
if(grade == "A-") return 3.667;
if(grade == "B+") return 3.333;
if(grade == "B") return 3.0;
if(grade == "B-") return 2.667;
if(grade == "C+") return 2.333;
if(grade == "C") return 2.0;
if(grade == "C-") return 1.667;
if(grade == "D+") return 1.333;
if(grade == "D") return 1.0;
if(grade == "D-") return 0.667;
if(grade == "F") return 0.0;
else return 0;
}
void course::set_course(std::string num, std::string grad, double hrs){
course_number = num;
grade = grad;
hours = hrs;
}
istream& operator >>(istream& ins, course& c){
c.input(ins);
return ins;
}
ostream& operator <<(ostream& outs, const course& c){
c.output(outs);
return outs;
}
void course::upper_course(){
for(size_t i =0; i<course_number.length(); ++i)
course_number[i] = toupper(course_number[i]);
}
course.hpp
#include<iostream>
#include<string>
#ifndef COURSE_H
#define COURSE_H
class course{
public:
course();
// Input and output functions
void input(std::istream& ins);
void output(std::ostream& outs)const;
// accessor functions
std::string get_course_number()const{
return course_number;
}
std::string get_grade()const{
return grade;
}
double get_hours()const{
return hours;
}
double get_number_grade()const;
// mutator function
void set_course(std::string num, std::string grad, double hrs);
//COMPARISON OPERATORS - ORDERS LISTS BY THEIR COURSE NUMBERS
bool operator < (const course& c)const{return course_number < c.course_number;}
bool operator <= (const course& c)const{return course_number <= c.course_number;}
bool operator > (const course& c)const{return course_number > c.course_number;}
bool operator >= (const course& c)const{return course_number >= c.course_number;}
bool operator == (const course& c)const{return course_number == c.course_number;}
bool operator != (const course& c)const{return course_number != c.course_number;}
private:
void upper_course();
std::string course_number;
std::string grade;
double hours;
};
std::istream& operator >>(std::istream& ins, course& c);
std::ostream& operator <<(std::ostream& outs, const course& c);
#endif
node.hpp
#ifndef NODE_H
#define NODE_H
#include "course.hpp"
class node{
public:
typedef course value_type;
// Universal constructor
node(value_type d = value_type(), node *l = NULL)
{data_field = d; link_field = l;}
// Mutator functions
void set_data(value_type d)
{data_field = d;}
void set_link(node *l)
{link_field = l;}
// Accessor functions
value_type data() const
{return data_field;}
node* link()
{return link_field;}
const node* link() const
{return link_field;}
private:
value_type data_field;
node* link_field;
};
#endif
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 info." << 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){
for(node* tmp = head; tmp != NULL; tmp = tmp->link()){
if((n.compare(tmp->data())) == 0){
tmp = tmp->link();
delete tmp;
}
else delete tmp;
}
}
void College::display(ostream& ofs){
for(node* tmp = head; tmp != NULL; tmp = tmp->link()){
cout << tmp->data() << endl;
}
}
double College::hours()const{
course c;
double total_hours = 0.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;
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
college.cpp:93:24: error: invalid operands to binary expression ("value_type (aka ·course') and char_traits char>, if(tmp->data() 'const std ::string" (aka allocator >')) n)( ·const basic-string-char, - 1 error generatedExplanation / Answer
In college.cpp
----------------------
AFTER:-
===============
void College::remove(const std::string& n){
for(node* tmp = head; tmp != NULL; tmp = tmp->link()){
string tmps = tmp->data().get_course_number();
if((n.compare(tmps)) == 0){
tmp = tmp->link();
delete tmp;
}
else delete tmp;
}
}
=================
now it will be without any compilation
===============
output:-
==========
Welcome to Your College Course Management.
Begin by entering your username: sri
Now Enter Your Full name:srgdf
Choose from the following options:
1) Add an additional course to your record.
2) Display all the courses taken thus far.
3) Remove a course from your college record.
4) Display the total hours thus far completed.
5) Display your current GPA.
6) Test your copy constructor.
0) Quit - saving all changes.
======================
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.