Jump to content

overloading my brain

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
Guest_dmegee_*

Guest_dmegee_*
  • Guests
Ok here's the deal I need to make a program that runs in this manner...
I have the majority of it done but I have some errors when I try to compile it.
Can anyone help me?

Create a class called Book, which has the following attributes:
1. title(string)
2. publisher (string)
3.Number of pages (int)
These should only be accesible through methods of the book class
then write two constructors for the book class
- a default constructor that sets the title and pulisher to some default value and the number of pages to zero
-an overloaded constructor that takes in three parameters and sets the title, publisher and number of pages appropriately.

I need to write one set method and one get method for each of the attributes of the book class.

finally overload the << so that a book object gets output in the following way:
TITLE: title
PUBLISHER: publisher
NUMBER OF PAGES: number of pages

then I create a class library. this has one attribute: a vector of books that will hold all the books in the library.

I write these member functions for the library class:
bool contains(string title)
void addbook(Book b)
void readfile(string filename)
then after I have defined those member functions I need to overload the following operators :
+=, to add a book to the library (maybe reuse addbook?)
==, to check if a book is in the library ( maybe reuse contains?)
<<,to output all the Books in the library( remember I have already defined this to output a single book)

So basically the program needs to be able to:
1. Read information about books from a file and add the books to the library's collection.
2.Search to see if a book is in the library
3.add a book to the library
4. list all the books in the library, with the publishers and number of pages

This is the code that I have so far, and I'm stuck. I need help on what is commented out and on the main function:


#include <iostream> //Basic Library of terms used by C++
#include <fstream> //Allows the program to read to and from files (I/O)
#include <string> //Allows the program to read strings
#include <vector> //Allows the use of vectors
#include <cctype>
#include <cstdlib>


using namespace std;

class Book //Class called Book
{
private: //private member variables
string title;
string publisher;
int numpages;

public:
friend void operator <<(ostream&, Book);
Book(){ title = ""; publisher = ""; numpages = 0;}
Book(string, string, int);
void set_title(string t) {title = t;}
void set_publisher(string p) {publisher = p;}
void set_numpages(int n){numpages = n;}
string get_title(){ return title;}
string get_publisher(){ return publisher;}
int get_numpages(){return numpages;}
};

class Library
{
public:
friend Library operator +=(Library, Book);
friend bool operator ==(Library, Library);
friend void operator <<(ostream&, Library);
bool contains(string);
void addBook(Book);
void readfile(string);
int size();
Book getBook(int);
private:
vector<Book> book_vector;
};

Book::Book(string new_title, string new_publisher, int new_numpages)
{
set_title(new_title);
set_publisher(new_publisher);
set_numpages(new_numpages);
}


void operator <<(ostream& outs, Book somebook)
{
outs << somebook.get_title() << endl ;
outs << somebook.get_publisher() << endl;
outs << somebook.get_numpages() << endl;
}


bool Library::contains(string test_title)
{
for (int i=0; i < book_vector.size() ; i++)
{
if (test_title == book_vector[i].get_title())
return 1;
}
return 0;
}


void Library::addBook(Book new_Book)
{
book_vector.push_back(new_Book);
}

void Library::readfile(string filename)
{
ifstream ifile;
ifile.open(filename.c_str());
string temptitle;
string temppublisher;
int temppages;
while(!ifile.eof())
{
/* filename >> temptitle;
filename >> temppublisher;
filename >> temppages;*/
Book tempbook(temptitle, temppublisher, temppages) ;
addBook(tempbook);
}
}


int Library::size()
{
return book_vector.size();
}

Library operator +=(Library oldlibrary, Book newbook)
{
oldlibrary.addBook(newbook);
return oldlibrary;
}

Book Library::getBook(int position)
{
return book_vector[position];
}

bool operator ==(Library a, Library b)
{
Book temp_book;
string temp_title;
if(a.size() != b.size())
return 0;
else
for (int i=0; i < a.size() ; i++)
{
temp_book=a.getBook(i);
temp_title=temp_book.get_title();
if (!b.contains(temp_title))
return 0;
}
return 1;

}

void operator <<(ostream& outs, Library temp_library)
{
for (int i=0; i << temp_library.size(); i++)
{
outs << temp_library.getBook(i) ;
}
}


int main()
{
/* char selection;
string filename;
cout<<"Welcome to Dave's woodworking library!"<<endl; //Intro
cout<<"Where everyone is getting hammered or nailed!"<<endl;
cout<<""<<endl;
cout<<"Please make your selction from the list below..."<<endl; //Asks user for input
cout<<"1. Read information about books from a file"<<endl;
cout<<"2. Find a book in the library"<<endl;
cout<<"3. Add your book to our library"<<endl;
cout<<"4. Print out the library's book collection"<<endl;
cout<<"5. Quit the program"<<endl; //Quits the program
cin>> selection;

switch(selection)
do{
case '1':
cout<< "Please enter your filename" <<endl;
cin>> filename;
getline
//read_info_books_file(filename);
break;

case '2':
cout<< "Please enter the name of your book"<<endl;
//cin>> title;
//getline

break;

case '3':
cout<<"Please enter the title, publisher and number of pages"<<endl;
//cin>> title,publisher,numberofpages;
//getline
//add to library
break;

case '4':
//prints out the library's book collection
break;

case '5':
cout<<"Please don't leave us! The books come alive at night!"<<endl;
break;

default:
cout<<"That's not a possible choice...you must have wood for brains!"<<endl;
}while (selection !=4);

*/
}

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
The biggest problem I can see is in your friend function <<.
It really needs to return ostream, so that you can have several << chained together.
Also, it's usually easier to help if we know what the compiler errors are.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Guest_dmegee_*

Guest_dmegee_*
  • Guests
Well...This is what I have done and I'm not sure that it's right...could someone look over this and give me some answers? The compiler errors are at the bottom. sorry I don't know how to put my code in nice little boxes


#include <iostream> //Basic Library of terms used by C++
#include <fstream> //Allows the program to read to and from files (I/O)
#include <string> //Allows the program to read strings
#include <vector> //Allows the use of vectors
#include <cctype>
#include <cstdlib>


using namespace std;

class Book //Class called Book
{
private: //private member variables
string title;
string publisher;
int numpages;

public:
friend void operator <<(ostream&, Book);
Book(){ title = ""; publisher = ""; numpages = 0;}
Book(string, string, int);
void set_title(string t) {title = t;}
void set_publisher(string p) {publisher = p;}
void set_numpages(int n){numpages = n;}
string get_title(){ return title;}
string get_publisher(){ return publisher;}
int get_numpages(){return numpages;}
};

class Library
{
public:
friend Library operator +=(Library, Book);
friend bool operator ==(Library, Library);
friend void operator <<(ostream&, Library);
bool contains(string);
void addBook(Book);
void readfile(string);
int size();
Book getBook(int);
private:
vector<Book> book_vector;
};

Book::Book(string new_title, string new_publisher, int new_numpages)
{
set_title(new_title);
set_publisher(new_publisher);
set_numpages(new_numpages);
}


void operator <<(ostream& outs, Book somebook)
{
outs << somebook.get_title() << endl ;
outs << somebook.get_publisher() << endl;
outs << somebook.get_numpages() << endl;
}


bool Library::contains(string test_title)
{
for (int i=0; i < book_vector.size() ; i++)
{
if (test_title == book_vector[i].get_title())
return 1;
}
return 0;
}


void Library::addBook(Book new_Book)
{
book_vector.push_back(new_Book);
}

void Library::readfile(string filename)
{
ifstream ifile;
ifile.open(filename.c_str());
string temptitle;
string temppublisher;
int temppages;
while(!ifile.eof())
{
ifile >> temptitle;
ifile >> temppublisher;
ifile >> temppages;
Book tempbook(temptitle, temppublisher, temppages) ;
addBook(tempbook);
}
}


int Library::size()
{
return book_vector.size();
}

Library operator +=(Library oldlibrary, Book newbook)
{
oldlibrary.addBook(newbook);
return oldlibrary;
}

Book Library::getBook(int position)
{
return book_vector[position];
}

bool operator ==(Library a, Library b)
{
Book temp_book;
string temp_title;
if(a.size() != b.size())
return 0;
else
for (int i=0; i < a.size() ; i++)
{
temp_book=a.getBook(i);
temp_title=temp_book.get_title();
if (!b.contains(temp_title))
return 0;
}
return 1;

}

void operator <<(ostream& outs, Library temp_library)
{
for (int i=0; i << temp_library.size(); i++)
{
outs << temp_library.getBook(i) ;
}
}

int main()
{

Library lib;
lib.readfile(filename);
lib.addBook(Book new_Book);
lib.contains(string test_title);
lib.size();
lib.getBook(int position);

Book b;
b.Book(string new_title, string new_publisher, int new_numpages)

char selection;
string filename;
cout<<"Welcome to Dave's woodworking library!"<<endl; //Intro
cout<<"Where everyone is getting hammered or nailed!"<<endl;
cout<<""<<endl;
cout<<"Please make your selction from the list below..."<<endl; //Asks user for input
cout<<"1. Read information about books from a file"<<endl;
cout<<"2. Find a book in the library"<<endl;
cout<<"3. Add your book to our library"<<endl;
cout<<"4. Print out the library's book collection"<<endl;
cout<<"5. Quit the program"<<endl; //Quits the program
cin>> selection;

switch(selection)
do{
case '1':
cout<< "Please enter your filename with the extension" <<endl;
cin>> filename;
void readfile(string filename);
Library lib;
lib.readfile(filename);


break;

case '2':
cout<< "Please enter the name of your book"<<endl;
cin>> title;
Library lib;
lib.contains(string test_title);
break;

case '3':
cout<<"Please enter the title, publisher and number of pages"<<endl;
cin>> title,publisher,numberofpages;
Library lib;
lib.addBook(Book new_Book);
break;

case '4':
//prints out the library's book collection
break;

case '5':
cout<<"Please don't leave us! The books come alive at night!"<<endl;
break;

default:
cout<<"That's not a possible choice...you must have wood for brains!"<<endl;
}while (selection !=4);

}

these are my errors:

:undefined identifer 'filename'
hello.cpp line 149 lib.readfile(filename);
: '(' expected
hello.cpp line 150 lib.addBook(Book new_Book);
: ';' expected
hello.cpp line151 lib.contains(string test_title);
: '(' expected
hello.cpp line 155 Book b;
:undefined identifier 'b'
hello.cpp line 156 b.Book(string new_title, string new_publisher,int new_numpages)
:',' expected
hello.cpp line 158 char selection;
:undefined identifier 'selection'
hello.cpp line 169 cin>>selection;
:undefined identifier 'selection'
hello.cpp line 171 switch(selection)
undefined identifier 'title'
hello.cpp line 185 cin>> title;
: '(' expected
hello.cpp line 186 lib.contains(string test_title);
: ';' expected
hello.cpp line 187 break;
:undefined identifier 'title'
hello.cpp line 191 cin>>title,publisher,numberofpages;
:'(' expected
hello.cpp line 192 lib.addbook(Book new_Book);
:';' expected
hello.cpp line 193 break;
:undefined identifier 'selection'
hello.cpp line 205 }while (selection !=4);

#4
Guest_NeedHelp_*

Guest_NeedHelp_*
  • Guests
It looks like you need to pass a Book class when you declare your library:

hello.cpp line 150 lib.addBook(Book new_Book);


#5
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
You put your code in a box using the word "CODE" in square brackets at the beginning, and "/CODE" in square brackets at the end.

As to the errors: Line 149: you have not declared "filename" or initialized it yet. It is declared on line 157. This is true of the other "undefined identifier" errors.

On lines 150 and 151, it looks like you copied the function declarations instead of deciding what to pass to it.

One hint in C++: Fix one bug at a time. Often, fixing one error will remove many error messages.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog