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(strin g t) {title = t;}
void set_publisher(s tring p) {publisher = p;}
void set_numpages(in t 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(stri ng new_title, string new_publisher, int new_numpages)
{
set_title(new_t itle);
set_publisher(n ew_publisher);
set_numpages(ne w_numpages);
}
void operator <<(ostream& outs, Book somebook)
{
outs << somebook.get_ti tle() << endl ;
outs << somebook.get_pu blisher() << endl;
outs << somebook.get_nu mpages() << endl;
}
bool Library::contai ns(string test_title)
{
for (int i=0; i < book_vector.siz e() ; i++)
{
if (test_title == book_vector[i].get_title())
return 1;
}
return 0;
}
void Library::addBoo k(Book new_Book)
{
book_vector.pus h_back(new_Book );
}
void Library::readfi le(string filename)
{
ifstream ifile;
ifile.open(file name.c_str());
string temptitle;
string temppublisher;
int temppages;
while(!ifile.eo f())
{
/* filename >> temptitle;
filename >> temppublisher;
filename >> temppages;*/
Book tempbook(tempti tle, temppublisher, temppages) ;
addBook(tempboo k);
}
}
int Library::size()
{
return book_vector.siz e();
}
Library operator +=(Library oldlibrary, Book newbook)
{
oldlibrary.addB ook(newbook);
return oldlibrary;
}
Book Library::getBoo k(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.get Book(i);
temp_title=temp _book.get_title ();
if (!b.contains(te mp_title))
return 0;
}
return 1;
}
void operator <<(ostream& outs, Library temp_library)
{
for (int i=0; i << temp_library.si ze(); i++)
{
outs << temp_library.ge tBook(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"<<en dl;
cout<<"5. Quit the program"<<endl; //Quits the program
cin>> selection;
switch(selectio n)
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);
*/
}
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(strin g t) {title = t;}
void set_publisher(s tring p) {publisher = p;}
void set_numpages(in t 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(stri ng new_title, string new_publisher, int new_numpages)
{
set_title(new_t itle);
set_publisher(n ew_publisher);
set_numpages(ne w_numpages);
}
void operator <<(ostream& outs, Book somebook)
{
outs << somebook.get_ti tle() << endl ;
outs << somebook.get_pu blisher() << endl;
outs << somebook.get_nu mpages() << endl;
}
bool Library::contai ns(string test_title)
{
for (int i=0; i < book_vector.siz e() ; i++)
{
if (test_title == book_vector[i].get_title())
return 1;
}
return 0;
}
void Library::addBoo k(Book new_Book)
{
book_vector.pus h_back(new_Book );
}
void Library::readfi le(string filename)
{
ifstream ifile;
ifile.open(file name.c_str());
string temptitle;
string temppublisher;
int temppages;
while(!ifile.eo f())
{
/* filename >> temptitle;
filename >> temppublisher;
filename >> temppages;*/
Book tempbook(tempti tle, temppublisher, temppages) ;
addBook(tempboo k);
}
}
int Library::size()
{
return book_vector.siz e();
}
Library operator +=(Library oldlibrary, Book newbook)
{
oldlibrary.addB ook(newbook);
return oldlibrary;
}
Book Library::getBoo k(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.get Book(i);
temp_title=temp _book.get_title ();
if (!b.contains(te mp_title))
return 0;
}
return 1;
}
void operator <<(ostream& outs, Library temp_library)
{
for (int i=0; i << temp_library.si ze(); i++)
{
outs << temp_library.ge tBook(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"<<en dl;
cout<<"5. Quit the program"<<endl; //Quits the program
cin>> selection;
switch(selectio n)
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);
*/
}
Comment