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? -
-
#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
17 3878
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.
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
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.
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: -
-
class shelf
-
{
-
private:
-
book books[50];
-
bool full ();
-
public:
-
shelf();
-
~shelf(){};
-
void input();
-
void output();
-
char alpha[2];
-
//bool full;
-
};
-
-
J
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: - 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().
- 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: -
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
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.
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
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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!
|
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
|
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...
|
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
|
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.
| |
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 :
|
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,...
|
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...
|
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)
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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();...
|
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...
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |