473,385 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

overloading my brain

2
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);

*/
}
Nov 20 '06 #1
3 1746
willakawill
1,646 1GB
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);

*/
}
You have created an endless loop within the switch statement for any entry of 6 or above
Nov 20 '06 #2
dmegee
2
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);
Nov 20 '06 #3
Banfa
9,065 Expert Mod 8TB
As a general rule the switch statement should not be mixed up (interleaved) with other statements like you have done with your switch and while. Also a switch statement needs { and }

[code]
switch(expression)
{
case 1:
// case 1 code
break;

case 2:
// case 1 code
break;

// other cases

default:
// default code
break;
}

I suspect you switch should be inside you while not the other way round.


I is possible to construct switchs with intervealed other statements but these are hacks (normally speed hacks) and only for the advanced user in very special cases.
Nov 20 '06 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

17
by: Terje Slettebų | last post by:
To round off my trilogy of "why"'s about PHP... :) If this subject have been discussed before, I'd appreciate a pointer to it. I again haven't found it in a search of the PHP groups. The PHP...
2
by: NKOBAYE027 | last post by:
I want to use the * operator of the parent class in the def'n of the * operator for my child class. Is there an elegant way to do this e.g. template<typename object> class foo : public...
9
by: daniel.w.gelder | last post by:
Hello, I am writing some code that converts one group of old obselete classes to another newer group, like this: Bag Convert(OldBag* b) { return Bag(b->x, b->y); } Shag Convert(OldShag* b) {...
15
by: Susan Baker | last post by:
Hello everybody, I'm new to C++ (I have some C background). I've read up on this topic a few times but it just dosen't seem to be sinking in. 1. Whats the difference between overloading and...
39
by: zeus | last post by:
I know function overloading is not supported in C. I have a few questions about this: 1. Why? is it from technical reasons? if so, which? 2. why wasn't it introduced to the ANSI? 3. Is there any...
10
by: chetan | last post by:
Hello All ! In c++, are basic data type int,char,long,..,etc. classes ? And if so, How can i overload new and delete operators for this this objects ? Thanks in advance ! Chetan
8
by: John Hardin | last post by:
All: The syntax for overloading the "+" and other simple binary math operators is pretty straightforward, but I can't seem to wrap my brain around the idea that overloading the "+" operator...
7
by: Marcos Stefanakopolus | last post by:
So this is ok: class foo { int myMethod(string, int) { ... } int myMethod(string, double) { ... } } But this is not: class foo {
2
by: Tom Smith | last post by:
I'm having difficulty with overloading ==, and it's making my brain melt - can you help...? What I want to have is: 1) A base class A with virtual operator== defined (no problem) 2) A class B...
16
by: Joseph Paterson | last post by:
Hello, I've created a class to store 2 dimensional matrices, and I've been trying to overload the operator, so access to the elements of the matrix is easy. I have 3 private variables, _m, _row...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.