473,779 Members | 2,083 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
17 3880
Hypnotik
87 New Member
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
9,208 Recognized Expert Moderator Expert
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
Hypnotik
87 New Member
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
9,208 Recognized Expert Moderator Expert
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
Hypnotik
87 New Member
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
9,208 Recognized Expert Moderator Expert
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
Hypnotik
87 New Member
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
9,208 Recognized Expert Moderator Expert
I recommend a new thread. This one's about done.
Jul 12 '07 #18

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

Similar topics

1
1600
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
1288
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
1821
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
2693
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
1604
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
2081
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
9633
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
10305
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...
1
10074
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9928
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
6724
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
5373
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...
0
5503
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3632
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2867
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.