By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,294 Members | 2,681 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,294 IT Pros & Developers. It's quick & easy.

Library (of books) program

P: 87
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?


Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <iostream>
  3. #include <ctype.h>
  4. using namespace std;
  5. struct book
  6. {
  7.     char title[25],author[25];
  8.     int ISBN,pages,year;
  9. };
  10. class shelf
  11. {
  12. private:
  13.     book books[50];
  14.  
  15. public:
  16.     shelf();
  17.     ~shelf(){};
  18.     void input();
  19.     void output();
  20.     char alpha[2];
  21.     bool full;
  22. };
  23. shelf::shelf()
  24. {
  25.     int i=0;
  26.     cout<<"constructing our shelves"<<endl;
  27.     for(i=0;i<50;++i)
  28.     {
  29.         books[i].title[i]='-';
  30.         books[i].author[i]='-';
  31.         books[i].ISBN=0;
  32.         books[i].pages=0;
  33.         books[i].year=0000;
  34.     }
  35.  
  36.     for (i=0;i<=2;++i)
  37.     {
  38.         alpha[i]='-';
  39.     }
  40.     full=false;
  41. };
  42. void shelf::input()
  43. {
  44.     int sel=0,i=0;
  45.     cout<<"This function allows the input of information for books."<<endl;
  46.     cout<<"What would you like to enter?"<<endl;
  47.     do
  48.     {
  49.         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;
  50.         cout<<"5) Enter the year the book was published."<<endl<<"6) Enter all of the information."<<endl;
  51.         cin>>sel;
  52.         switch(sel)
  53.         {
  54.         case 1:
  55.             cout<<"Enter the authors name."<<endl;
  56.             cin.getline(books[i].author,20,'/n');
  57.             cin.ignore();
  58.             break;
  59.         case 2:
  60.             cout<<"Enter the title of the book."<<endl;
  61.             cin.getline (books[i].title,20,'/n');
  62.             cin.ignore();
  63.             break;
  64.         case 3:
  65.             cout<<"Enter the ISBN of the book."<<endl;
  66.             cin>>books[i].ISBN;
  67.             break;
  68.         case 4:
  69.             cout<<"Enter the number of pages."<<endl;
  70.             cin>>books[i].pages;
  71.             break;
  72.         case 5:
  73.             cout<<"Enter the year the book was published."<<endl;
  74.             cin>>books[i].year;
  75.             break;
  76.         case 6:
  77.             cout<<"Enter the authors name."<<endl;
  78.             {
  79.                 cin.ignore();
  80.                 cin.getline(books[i].author,30,'\n');
  81.                 cout<<books[i].author<<endl;
  82.  
  83.             }
  84.             cout<<"Enter the title of the book."<<endl;
  85.             {
  86.                 cin.getline (books[i].title,30,'\n');
  87.                 cout<<books[i].title<<endl;
  88.             }
  89.             cout<<"Enter the ISBN of the book."<<endl;
  90.             cin>>books[i].ISBN;
  91.             cout<<books[i].ISBN<<endl;
  92.             cout<<"Enter the number of pages."<<endl;
  93.             cin>>books[i].pages;
  94.             cout<<books[i].pages<<endl;
  95.             cout<<"Enter the year the book was published."<<endl;
  96.             cin>>books[i].year;
  97.             cout<<books[i].year<<endl;
  98.             break;
  99.         default:
  100.             cout<<"Make a selection from the menu."<<endl;
  101.             break;
  102.         }
  103.     }
  104.     while (sel<1||sel>6);
  105.  
  106. }
  107. void shelf::output()
  108. {
  109.     int i=0;
  110.     char let;
  111.     cout<<"Enter the letter for which you would like to display titles: "<<endl;
  112.     cin>>let;
  113.     let=toupper(let);
  114.     if (books[i].title[i]==let)
  115.     {
  116.         cout<<"Author         Title        ISBN        #Pages          Published"<<endl;
  117.         cout<<books[i].author<<"       "<<books[i].title<<"       "<<books[i].ISBN<<"       "<<books[i].pages<<"       "<<books[i].year<<endl;
  118.     }
  119.     else
  120.     cout<<"No book titles found which begin with the letter "<<let<<endl;
  121. }
  122. void main()
  123. {
  124.     char ans='Y';
  125.     int i=0;
  126.     shelf library[25];
  127.     do
  128.     {
  129.         cout<<"Do you have a book to enter? Enter Y for yes or # for no."<<endl;
  130.         cin>>ans;
  131.         ans=toupper(ans);
  132.         library[i].input();
  133.         library[i].output();
  134.         ++i;
  135.     }
  136.     while (ans!='#');
  137. //    library[i].output();
  138.     cout<<"Exiting program."<<endl;
  139. }
  140.  
  141.  
Thanks,
J
Jul 10 '07 #1
Share this Question
Share on Google+
17 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
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.
Jul 10 '07 #2

P: 87
I have declared in the class bool full. That is what I was planning on using for the true/false....determiner. 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
Jul 10 '07 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
have declared in the class bool full. That is what I was planning on using for the true/false....determiner. 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.
Jul 10 '07 #4

P: 87
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:
Expand|Select|Wrap|Line Numbers
  1.  
  2. class shelf
  3. {
  4. private:
  5.     book books[50];
  6.     bool full ();
  7. public:
  8.     shelf();
  9.     ~shelf(){};
  10.     void input();
  11.     void output();
  12.     char alpha[2];
  13.     //bool full;
  14. };
  15.  
  16.  
J
Jul 10 '07 #5

100+
P: 147
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:
Expand|Select|Wrap|Line Numbers
  1. private:
  2.   int numOfBooks;
  3. public:
  4.   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().
Jul 10 '07 #6

P: 87

Expand|Select|Wrap|Line Numbers
  1. private:
  2.   int numOfBooks;
  3. public:
  4.   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.....however, 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:

Expand|Select|Wrap|Line Numbers
  1. private: 
  2.    books[50];
  3. public: 
  4.    bool full(){ return (books[50]); }
  5.  

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

Thanks,
J
Jul 10 '07 #7

weaknessforcats
Expert Mod 5K+
P: 9,197
This is not what Scruggsy said:
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.
Jul 10 '07 #8

P: 87
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
Jul 10 '07 #9

100+
P: 147
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.
Jul 10 '07 #10

P: 87
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
Jul 10 '07 #11

weaknessforcats
Expert Mod 5K+
P: 9,197
Yes.

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

That may cause you to rehtink using a vector<book>.
Jul 11 '07 #12

P: 87
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.

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <iostream>
  3. #include <ctype.h>
  4. using namespace std;
  5. struct book
  6. {
  7.     char title[25],author[25];
  8.     int ISBN,pages,year;
  9. };
  10. class shelf
  11. {
  12. private:
  13.     book books[50];
  14.     int num_books;
  15. public:
  16.     shelf();
  17.     ~shelf(){};
  18.     void input();
  19.     void output();
  20.     char alpha[2];
  21.     bool full();
  22. };
  23. shelf::shelf()
  24. {
  25.     int i=0;
  26.     cout<<"constructing our shelves"<<endl;
  27.     for(i=0;i<25;++i)
  28.     {
  29.         books[i].title[i]='-';
  30.         books[i].author[i]='-';
  31.         books[i].ISBN=0;
  32.         books[i].pages=0;
  33.         books[i].year=0000;
  34.     }
  35.  
  36.     for (i=0;i<2;++i)
  37.     {
  38.         alpha[i]='-';
  39.     }
  40.     num_books=0;
  41. };
  42. bool shelf::full()
  43. {
  44.     return (num_books==50);
  45. }
  46. void shelf::input()
  47. {
  48.     int sel=0,i=0;
  49.     cout<<"This function allows the input of information for books."<<endl;
  50.     cout<<"What would you like to enter?"<<endl;
  51.     do
  52.     {
  53.         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;
  54.         cout<<"5) Enter the year the book was published."<<endl<<"6) Enter all of the information."<<endl<<"7) Exit"<<endl;
  55.         cin>>sel;
  56.         switch(sel)
  57.         {
  58.         case 1:
  59.             cout<<"Enter the authors name."<<endl;
  60.             cin.getline(books[i].author,20,'/n');
  61.             cin.ignore();
  62.             break;
  63.         case 2:
  64.             cout<<"Enter the title of the book."<<endl;
  65.             cin.getline (books[i].title,20,'/n');
  66.             cin.ignore();
  67.             break;
  68.         case 3:
  69.             cout<<"Enter the ISBN of the book."<<endl;
  70.             cin>>books[i].ISBN;
  71.             break;
  72.         case 4:
  73.             cout<<"Enter the number of pages."<<endl;
  74.             cin>>books[i].pages;
  75.             break;
  76.         case 5:
  77.             cout<<"Enter the year the book was published."<<endl;
  78.             cin>>books[i].year;
  79.             break;
  80.         case 6:
  81.             cout<<"Enter the authors name."<<endl;
  82.             cin.ignore();
  83.             cin.getline(books[i].author,30,'\n');
  84.             cout<<books[i].author<<endl;                                    
  85.             cout<<"Enter the title of the book."<<endl;
  86.             cin.getline (books[i].title,30,'\n');
  87.             cout<<books[i].title<<endl;                                        
  88.             cout<<"Enter the ISBN of the book."<<endl;
  89.             cin>>books[i].ISBN;
  90.             cout<<books[i].ISBN<<endl;
  91.             cout<<"Enter the number of pages."<<endl;
  92.             cin>>books[i].pages;
  93.             cout<<books[i].pages<<endl;
  94.             cout<<"Enter the year the book was published."<<endl;
  95.             cin>>books[i].year;
  96.             cout<<books[i].year<<endl;
  97.             break;
  98.         case 7:
  99.             cout<<"Exiting."<<endl;
  100.             break;
  101.         default:
  102.             cout<<"Make a selection from the menu."<<endl;
  103.             break;
  104.         }
  105.     }
  106.     while (sel<1||sel>7);
  107.  
  108. }
  109. void shelf::output()
  110. {
  111.     int i=0;
  112.     char let;
  113.     cout<<"Enter the letter for which you would like to display titles: "<<endl;
  114.     cin>>let;
  115.     let=toupper(let);
  116.     if (books[i].title[i]==let)
  117.     {
  118.         cout<<"Author        Title        ISBN        #Pages        Published"<<endl;
  119.         cout<<books[i].author<<"     "<<books[i].title<<"     "<<books[i].ISBN<<"     "<<books[i].pages<<"     "<<books[i].year<<endl;
  120.     }
  121.     else
  122.         cout<<"No book titles found which begin with the letter "<<let<<endl;
  123. }
  124. void main()
  125. {
  126.     char ans;
  127.     int i=0;
  128.     shelf library[25];
  129.     do
  130.     {
  131.         cout<<"Do you have a book to enter? Enter Y for yes or N for no."<<endl;
  132.         cin>>ans;
  133.         ans=toupper(ans);
  134.         cout<<ans<<endl;
  135.     //    library[i].input();
  136.         library[i].output();
  137.     }
  138.     while (ans=='Y');
  139.     cout<<"Exiting program."<<endl;
  140. }
  141.  
  142.  
Thanks,
J
Jul 12 '07 #13

weaknessforcats
Expert Mod 5K+
P: 9,197
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.
Jul 12 '07 #14

P: 87
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:

Expand|Select|Wrap|Line Numbers
  1.  
  2. void main()
  3. {
  4.     char ans;
  5.     int i=0;
  6.     shelf library[25];
  7.     do
  8.     {
  9.         cout<<"Do you have a book to enter? Enter Y for yes or N for no."<<endl;
  10.         cin>>ans;
  11.         ans=toupper(ans);
  12.         cout<<ans<<endl;
  13.         library[i].input();                        // if this is removed I can exit with ans=n
  14.         library[i].output();
  15.     }
  16.     while (ans=='Y');
  17.     cout<<"Exiting program."<<endl;
  18. }
  19.  
  20.  
Thanks,
J
Jul 12 '07 #15

weaknessforcats
Expert Mod 5K+
P: 9,197
When the user enters N that means no more shelves are to be entered. So don't call shelf::input():

Expand|Select|Wrap|Line Numbers
  1. ;
  2.     do
  3.     {
  4.         cout<<"Do you have a book to enter? Enter Y for yes or N for no."<<endl;
  5.         cin>>ans;
  6.         ans=toupper(ans);
  7.         cout<<ans<<endl;
  8.         if (ans == 'Y')
  9.         {
  10.                 library[i].input();      
  11.                 library[i].output();
  12.          }
  13.     }
  14.     while (ans=='Y');
  15.     cout<<"Exiting program."<<endl;
  16. }
  17.  
  18.  
  19.  
Jul 12 '07 #16

P: 87
Ugh....my brain is fried. Ofcourse I don't want to enter any info....if I don't have a book.

I appreciate it, my instructor is out of town and I'm working on 4 programs at the same time.

Thanks again, I'm sure I'll be posting more in this thread.

Jeff
Jul 12 '07 #17

weaknessforcats
Expert Mod 5K+
P: 9,197
I recommend a new thread. This one's about done.
Jul 12 '07 #18

Post your reply

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