473,769 Members | 3,872 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Library (of books) program

87 New Member
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
17 3878
weaknessforcats
9,208 Recognized Expert Moderator Expert
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
Hypnotik
87 New Member
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
Jul 10 '07 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
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.
Jul 10 '07 #4
Hypnotik
87 New Member
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
scruggsy
147 New Member
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
Hypnotik
87 New Member

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

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
9,208 Recognized Expert Moderator Expert
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
Hypnotik
87 New Member
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
scruggsy
147 New Member
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

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

Similar topics

1
1599
by: cnwilks | last post by:
We're in the process of trying to manage an inventory of library books using access, and were wondering about the following: 1. Bar code scanning hardware-cost/setup, etc. 2. Record input-any resources to populate records with UPC/ISBN codes without having to key in all data manually. Thanks in advance!
2
1287
by: Kenneth P | last post by:
Hi, This is perhaps not the right forum to discuss this matter but it's about the sample library of VS.NET2003. I have a swedish OS Windows 2000 Professional and accordingly all messages are given in swedish. So if some swedes are watching this thread, they'll understand, to the rest of you, I'll translate it to the best of my knowledge. On every machine boot this message dialog OK/Cancel pops up with the the
10
1820
by: mwt | last post by:
So in a further attempt to learn some Python, I've taken the little Library program (http://groups.google.com/group/comp.lang.python/browse_thread/thread/f6a9ccf1bc136f84) I wrote and added several features to it. Readers now quit when they've read all the books in the Library. Books know how many times they've been read. Best of all, you can now create your own list of books to read! Again, the point of all this is to get used to...
10
2691
by: Julian | last post by:
I get the following error when i try to link a fortran library to a c++ code in .NET 2005. LINK : fatal error LNK1104: cannot open file 'libc.lib' the code was working fine when built using .NET2003. also, when I do not try to link the fortran library (just to see if that was the cause), it builds the exe without any problems. i don't even know how to begin addressing this problem...any help would be
1
1603
by: fallleaf | last post by:
i make library, this library function is create circula-queue in shared memory and attach, queue put, queue get, etc... sample program make, 1 sample program use library, test make queue in shared memory. 2 sample program use library, test attach queue in shared memory which created 1 sample program. result is success. Then A program use this library, and B program use this library.
5
1532
by: Harish Sundararaj | last post by:
Hi evr1, I have a program which im not sure whether the bug is with the program or the linux printf library....if some one could help me debug it..it'll be very helpful for me. the program is at : http://freeshell.in/~harish/files/printbuffer/tmbo.h.. the whole tar file is at :
3
2079
by: jchimanzi | last post by:
I am trying to develop a small program which does library books. I have tried the code below but it still does not work. Can someone help me to debug the program and advice accordingly. ii) // This program get or request the customer to enter their details and or details of the book. # include <iostream.h> # include “Library.h” int Main () { Void display Customer details (Char*Char, String); Void display books details (Char*Char,...
2
4433
by: Grant Edwards | last post by:
I'm trying to figure out how to use the gnu readline library so that when my program is prompting the user for input there is line editing and history support. I've read and re-read the documentation for the "readline" module in the standard library and I still can't figure out how to use the module or even if the module is intended to do what I want. The example code all seems to be about on how to modify the behavior of an...
1
4710
by: Kusno Doang | last post by:
Dear all, In our AS400 system, there is a program, 'PrgSend" located in MyLib library. That program only reads from PF file that comes from FTP, "TMPData" and sends data to Real table. I have the menu program with this command : 0001 RUNQRY *N TMPData 0002 call PrgSend 0003 WRKSPLF SELECT(*CURRENT *ALL *ALL PrgSend)
0
9579
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10206
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9851
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8863
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7403
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6662
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5293
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3949
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3556
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.