Library (of books) program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Hypnotik
    New Member
    • Jun 2007
    • 87

    Library (of books) program

    Hello everyone. I'm working on a program which uses a class (Shelf) and a struct (Book). In the main program we declare an array (Library[25]) which is of type shelf. The program takes in various info about books (author, title, isbn, etc) and then allows you to search for books starting with a certain letter, and in turn displays those books and the info on them.

    At this point I can enter the info, and display the info for 1 book. I'm confused about where I should look to enter more info. I'm assuming it should be in my main. Do I need to increment i (my counter) at the end of the loop so the info is not copying over itself?


    Code:
    #include <iostream>
    #include <ctype.h>
    using namespace std;
    struct book
    {
    	char title[25],author[25];
    	int ISBN,pages,year;
    };
    class shelf
    {
    private:
    	book books[50];
    
    public:
    	shelf();
    	~shelf(){};
    	void input();
    	void output();
    	char alpha[2];
    	bool full;
    };
    shelf::shelf()
    {
    	int i=0;
    	cout<<"constructing our shelves"<<endl;
    	for(i=0;i<50;++i)
    	{
    		books[i].title[i]='-';
    		books[i].author[i]='-';
    		books[i].ISBN=0;
    		books[i].pages=0;
    		books[i].year=0000;
    	}
    	
    	for (i=0;i<=2;++i)
    	{
    		alpha[i]='-';
    	}
    	full=false;
    };
    void shelf::input()
    {
    	int sel=0,i=0;
    	cout<<"This function allows the input of information for books."<<endl;
    	cout<<"What would you like to enter?"<<endl;
    	do
    	{
    		cout<<"1) Enter authors name."<<endl<<"2) Enter the title of the book."<<endl<<"3) Enter the ISBN."<<endl<<"4) Enter the number of pages."<<endl;
    		cout<<"5) Enter the year the book was published."<<endl<<"6) Enter all of the information."<<endl;
    		cin>>sel;
    		switch(sel)
    		{
    		case 1:
    			cout<<"Enter the authors name."<<endl;
    			cin.getline(books[i].author,20,'/n');
    			cin.ignore();
    			break;
    		case 2:
    			cout<<"Enter the title of the book."<<endl;
    			cin.getline (books[i].title,20,'/n');
    			cin.ignore();
    			break;
    		case 3:
    			cout<<"Enter the ISBN of the book."<<endl;
    			cin>>books[i].ISBN;
    			break;
    		case 4:
    			cout<<"Enter the number of pages."<<endl;
    			cin>>books[i].pages;
    			break;
    		case 5:
    			cout<<"Enter the year the book was published."<<endl;
    			cin>>books[i].year;
    			break;
    		case 6:
    			cout<<"Enter the authors name."<<endl;
    			{
    				cin.ignore();
    				cin.getline(books[i].author,30,'\n');
    				cout<<books[i].author<<endl;
    				
    			}
    			cout<<"Enter the title of the book."<<endl;
    			{
    				cin.getline (books[i].title,30,'\n');
    				cout<<books[i].title<<endl;
    			}
    			cout<<"Enter the ISBN of the book."<<endl;
    			cin>>books[i].ISBN;
    			cout<<books[i].ISBN<<endl;
    			cout<<"Enter the number of pages."<<endl;
    			cin>>books[i].pages;
    			cout<<books[i].pages<<endl;
    			cout<<"Enter the year the book was published."<<endl;
    			cin>>books[i].year;
    			cout<<books[i].year<<endl;
    			break;
    		default:
    			cout<<"Make a selection from the menu."<<endl;
    			break;
    		}
    	}
    	while (sel<1||sel>6);
    
    }
    void shelf::output()
    {
    	int i=0;
    	char let;
    	cout<<"Enter the letter for which you would like to display titles: "<<endl;
    	cin>>let;
    	let=toupper(let);
    	if (books[i].title[i]==let)
    	{
    		cout<<"Author         Title        ISBN        #Pages          Published"<<endl;
    		cout<<books[i].author<<"       "<<books[i].title<<"       "<<books[i].ISBN<<"       "<<books[i].pages<<"       "<<books[i].year<<endl;
    	}
    	else
    	cout<<"No book titles found which begin with the letter "<<let<<endl;
    }
    void main()
    {
    	char ans='Y';
    	int i=0;
    	shelf library[25];
    	do
    	{
    		cout<<"Do you have a book to enter? Enter Y for yes or # for no."<<endl;
    		cin>>ans;
    		ans=toupper(ans);
    		library[i].input();
    		library[i].output();
    		++i;
    	}
    	while (ans!='#');
    //	library[i].output();
    	cout<<"Exiting program."<<endl;
    }
    Thanks,
    J
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    You have some problems in your class design.


    Your shelf class has a fixed array of 50 books burt nowhere in the class is there a data member for how many books are on the shelf.

    You need a data member for that.

    Each time you add a book to the shelf, you access this data member, add, and update the data member. Then using the original value add the book at that location in the array. The shelf always needs to know how many books it has.

    You need a member function that returns true if the shelf is full (50 books) and false otherwise.

    You need a member function that positions to any book. This position is another data member. This way you call call this function to set the position in your book array.

    You need a member function that moves to the next book.

    etc...

    Or, you could use a vector<book> in your shelf class and write member function in the shelf that work with this vector.

    Comment

    • Hypnotik
      New Member
      • Jun 2007
      • 87

      #3
      I have declared in the class bool full. That is what I was planning on using for the true/false....determ iner. Is that incorrect? I figured each time I called the input function I would count and also call the full function, to determine whether the shelf was full or not.


      J

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Originally posted by Hypnotik
        have declared in the class bool full. That is what I was planning on using for the true/false....determ iner. Is that incorrect? I figured each time I called the input function I would count and also call the full function, to determine whether the shelf was full or not.
        the bool full is not a member function. It is just a public data member. Avoid public data members. The reason is the name full gets used in main() then if you ever need to change how you implement full, you will also need to change all the user code. Users don't like that.

        Personally, since you have a hard-coded limit of 50 books, I would write a member function name Full() to test the number of books in the array (using a data member that's not there yet) and if that number is 50, I would return true.

        Comment

        • Hypnotik
          New Member
          • Jun 2007
          • 87

          #5
          Originally posted by weaknessforcats
          the bool full is not a member function. It is just a public data member. Avoid public data members. The reason is the name full gets used in main() then if you ever need to change how you implement full, you will also need to change all the user code. Users don't like that.

          Personally, since you have a hard-coded limit of 50 books, I would write a member function name Full() to test the number of books in the array (using a data member that's not there yet) and if that number is 50, I would return true.

          I guess I'm slightly confused. When you say a member function do you mean a private member function as opposed to the public?

          such as:
          Code:
          class shelf
          {
          private:
          	book books[50];
          	bool full ();
          public:
          	shelf();
          	~shelf(){};
          	void input();
          	void output();
          	char alpha[2];
          	//bool full;
          };
          J

          Comment

          • scruggsy
            New Member
            • Mar 2007
            • 147

            #6
            Originally posted by Hypnotik
            I guess I'm slightly confused. When you say a member function do you mean a private member function as opposed to the public?
            J
            You have bool full; which is a public bool member, not a function.
            The suggestion is to polish up your class design by including a private member which stores the number of books, and a public bool member function which returns whether or not the maximum storable number of books has been reached:
            Code:
            private:
              int numOfBooks;
            public:
              bool full() { return ( numOfBooks == 50); }
            Now your main() routine can ask the shelf if it is full without having to know anything about how many books are allowed on the shelf or how the books are stored. You can change the limit on numOfBooks or change the implementation completely without having to alter main().

            Comment

            • Hypnotik
              New Member
              • Jun 2007
              • 87

              #7
              Originally posted by scruggsy

              Code:
              private:
                int numOfBooks;
              public:
                bool full() { return ( numOfBooks == 50); }
              Now your main() routine can ask the shelf if it is full without having to know anything about how many books are allowed on the shelf or how the books are stored. You can change the limit on numOfBooks or change the implementation completely without having to alter main().
              Ok that makes more sense.....howev er, I believe in the problem I have to have an array of 50 books as a private member. I don't have the assignment right in front of me at the moment.

              would the code be:

              Code:
              private: 
                 books[50];
              public: 
                 bool full(){ return (books[50]); }

              I believe I am stating the same thing as you were....or am I not?

              Thanks,
              J

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                This is not what Scruggsy said:
                Originally posted by Hypnotik
                private:
                books[50];
                public:
                bool full(){ return (books[50]); }
                The array is an array of book objects. You can't return a book as a bool. Further, book[50] is the 50th book in the array. If you have 5 books, the array has room for 45 more. Only when you have 50 books is the array full.

                You absolutely need a private class member for the number of books in the array. When that number is 50, the array is full. Recheck Scruggsy's code and you'll see it.

                Comment

                • Hypnotik
                  New Member
                  • Jun 2007
                  • 87

                  #9
                  Originally posted by weaknessforcats
                  This is not what Scruggsy said:


                  The array is an array of book objects. You can't return a book as a bool. Further, book[50] is the 50th book in the array. If you have 5 books, the array has room for 45 more. Only when you have 50 books is the array full.

                  You absolutely need a private class member for the number of books in the array. When that number is 50, the array is full. Recheck Scruggsy's code and you'll see it.

                  OK......thank you for explaining more indepth. One other thing for my clarification.. ...the private class member for the number of books is counting the books right?

                  J

                  Comment

                  • scruggsy
                    New Member
                    • Mar 2007
                    • 147

                    #10
                    Originally posted by Hypnotik
                    OK......thank you for explaining more indepth. One other thing for my clarification.. ...the private class member for the number of books is counting the books right?

                    J
                    Yes, that's the point.
                    How do you keep track of how many books are in the shelf?
                    Increment numOfBooks each time a new book is added.
                    Again, this is forcing your class to take care of the details of the implementation, rather than letting external code like main() mess with them.

                    Comment

                    • Hypnotik
                      New Member
                      • Jun 2007
                      • 87

                      #11
                      Got it, I was trying to count the books added in main (or atleast I was planning to). I am assuming in the constructor I need to initialize the num_books variable, and then I will increment it within the input function?

                      J

                      Comment

                      • weaknessforcats
                        Recognized Expert Expert
                        • Mar 2007
                        • 9214

                        #12
                        Yes.

                        Then you can tackle how you remove a book from the array.

                        That may cause you to rehtink using a vector<book>.

                        Comment

                        • Hypnotik
                          New Member
                          • Jun 2007
                          • 87

                          #13
                          I actually do not have to remove any books from the array. Basically all I have to do is add books, decide whether the shelf is full, and display the books and info that begin with a specified letter.


                          I for some reason am unable to get out of my menu that prints out in main. When I remove the input function call, it exits. I've stared at the function for awhile and I can't see the problem.

                          Code:
                          #include <iostream>
                          #include <ctype.h>
                          using namespace std;
                          struct book
                          {
                          	char title[25],author[25];
                          	int ISBN,pages,year;
                          };
                          class shelf
                          {
                          private:
                          	book books[50];
                          	int num_books;
                          public:
                          	shelf();
                          	~shelf(){};
                          	void input();
                          	void output();
                          	char alpha[2];
                          	bool full();
                          };
                          shelf::shelf()
                          {
                          	int i=0;
                          	cout<<"constructing our shelves"<<endl;
                          	for(i=0;i<25;++i)
                          	{
                          		books[i].title[i]='-';
                          		books[i].author[i]='-';
                          		books[i].ISBN=0;
                          		books[i].pages=0;
                          		books[i].year=0000;
                          	}
                          	
                          	for (i=0;i<2;++i)
                          	{
                          		alpha[i]='-';
                          	}
                          	num_books=0;
                          };
                          bool shelf::full()
                          {
                          	return (num_books==50);
                          }
                          void shelf::input()
                          {
                          	int sel=0,i=0;
                          	cout<<"This function allows the input of information for books."<<endl;
                          	cout<<"What would you like to enter?"<<endl;
                          	do
                          	{
                          		cout<<"1) Enter authors name."<<endl<<"2) Enter the title of the book."<<endl<<"3) Enter the ISBN."<<endl<<"4) Enter the number of pages."<<endl;
                          		cout<<"5) Enter the year the book was published."<<endl<<"6) Enter all of the information."<<endl<<"7) Exit"<<endl;
                          		cin>>sel;
                          		switch(sel)
                          		{
                          		case 1:
                          			cout<<"Enter the authors name."<<endl;
                          			cin.getline(books[i].author,20,'/n');
                          			cin.ignore();
                          			break;
                          		case 2:
                          			cout<<"Enter the title of the book."<<endl;
                          			cin.getline (books[i].title,20,'/n');
                          			cin.ignore();
                          			break;
                          		case 3:
                          			cout<<"Enter the ISBN of the book."<<endl;
                          			cin>>books[i].ISBN;
                          			break;
                          		case 4:
                          			cout<<"Enter the number of pages."<<endl;
                          			cin>>books[i].pages;
                          			break;
                          		case 5:
                          			cout<<"Enter the year the book was published."<<endl;
                          			cin>>books[i].year;
                          			break;
                          		case 6:
                          			cout<<"Enter the authors name."<<endl;
                          			cin.ignore();
                          			cin.getline(books[i].author,30,'\n');
                          			cout<<books[i].author<<endl;									
                          			cout<<"Enter the title of the book."<<endl;
                          			cin.getline (books[i].title,30,'\n');
                          			cout<<books[i].title<<endl;										
                          			cout<<"Enter the ISBN of the book."<<endl;
                          			cin>>books[i].ISBN;
                          			cout<<books[i].ISBN<<endl;
                          			cout<<"Enter the number of pages."<<endl;
                          			cin>>books[i].pages;
                          			cout<<books[i].pages<<endl;
                          			cout<<"Enter the year the book was published."<<endl;
                          			cin>>books[i].year;
                          			cout<<books[i].year<<endl;
                          			break;
                          		case 7:
                          			cout<<"Exiting."<<endl;
                          			break;
                          		default:
                          			cout<<"Make a selection from the menu."<<endl;
                          			break;
                          		}
                          	}
                          	while (sel<1||sel>7);
                          
                          }
                          void shelf::output()
                          {
                          	int i=0;
                          	char let;
                          	cout<<"Enter the letter for which you would like to display titles: "<<endl;
                          	cin>>let;
                          	let=toupper(let);
                          	if (books[i].title[i]==let)
                          	{
                          		cout<<"Author        Title        ISBN        #Pages        Published"<<endl;
                          		cout<<books[i].author<<"     "<<books[i].title<<"     "<<books[i].ISBN<<"     "<<books[i].pages<<"     "<<books[i].year<<endl;
                          	}
                          	else
                          		cout<<"No book titles found which begin with the letter "<<let<<endl;
                          }
                          void main()
                          {
                          	char ans;
                          	int i=0;
                          	shelf library[25];
                          	do
                          	{
                          		cout<<"Do you have a book to enter? Enter Y for yes or N for no."<<endl;
                          		cin>>ans;
                          		ans=toupper(ans);
                          		cout<<ans<<endl;
                          	//	library[i].input();
                          		library[i].output();
                          	}
                          	while (ans=='Y');
                          	cout<<"Exiting program."<<endl;
                          }
                          Thanks,
                          J

                          Comment

                          • weaknessforcats
                            Recognized Expert Expert
                            • Mar 2007
                            • 9214

                            #14
                            Originally posted by Hypnotik
                            case 7:
                            cout<<"Exiting. "<<endl;
                            break;
                            default:
                            cout<<"Make a selection from the menu."<<endl;
                            break;
                            }
                            }
                            while (sel<1||sel>7);
                            The exit is a 7. At 7 you break out of the switch. But you don't break out of the while until you enter a number > 7. Maybe you need:

                            while (sel != 7)

                            instead.

                            Comment

                            • Hypnotik
                              New Member
                              • Jun 2007
                              • 87

                              #15
                              Originally posted by weaknessforcats
                              The exit is a 7. At 7 you break out of the switch. But you don't break out of the while until you enter a number > 7. Maybe you need:

                              while (sel != 7)

                              instead.
                              The menu in the main program is what I can't exit out of if there is an input function call:

                              Code:
                              void main()
                              {
                              	char ans;
                              	int i=0;
                              	shelf library[25];
                              	do
                              	{
                              		cout<<"Do you have a book to enter? Enter Y for yes or N for no."<<endl;
                              		cin>>ans;
                              		ans=toupper(ans);
                              		cout<<ans<<endl;
                              		library[i].input();						// if this is removed I can exit with ans=n
                              		library[i].output();
                              	}
                              	while (ans=='Y');
                              	cout<<"Exiting program."<<endl;
                              }
                              Thanks,
                              J

                              Comment

                              Working...