overloading my brain

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dmegee
    New Member
    • Nov 2006
    • 2

    overloading my brain

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

    */
    }
  • willakawill
    Top Contributor
    • Oct 2006
    • 1646

    #2
    Originally posted by dmegee
    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);

    */
    }
    You have created an endless loop within the switch statement for any entry of 6 or above

    Comment

    • dmegee
      New Member
      • Nov 2006
      • 2

      #3
      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(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())
      {
      ifile >> temptitle;
      ifile >> temppublisher;
      ifile >> 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()
      {

      Library lib;
      lib.readfile(fi lename);
      lib.addBook(Boo k new_Book);
      lib.contains(st ring 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"<<en dl;
      cout<<"5. Quit the program"<<endl; //Quits the program
      cin>> selection;

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


      break;

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

      case '3':
      cout<<"Please enter the title, publisher and number of pages"<<endl;
      cin>> title,publisher ,numberofpages;
      Library lib;
      lib.addBook(Boo k 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(fi lename);
      : '(' expected
      hello.cpp line 150 lib.addBook(Boo k new_Book);
      : ';' expected
      hello.cpp line151 lib.contains(st ring test_title);
      : '(' expected
      hello.cpp line 155 Book b;
      :undefined identifier 'b'
      hello.cpp line 156 b.Book(string new_title, string new_publisher,i nt 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(selectio n)
      undefined identifier 'title'
      hello.cpp line 185 cin>> title;
      : '(' expected
      hello.cpp line 186 lib.contains(st ring test_title);
      : ';' expected
      hello.cpp line 187 break;
      :undefined identifier 'title'
      hello.cpp line 191 cin>>title,publ isher,numberofp ages;
      :'(' expected
      hello.cpp line 192 lib.addbook(Boo k new_Book);
      :';' expected
      hello.cpp line 193 break;
      :undefined identifier 'selection'
      hello.cpp line 205 }while (selection !=4);

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        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(expressi on)
        {
        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.

        Comment

        Working...