Solved

create a class called "book" using c++ that has a private data of :

Posted on 2006-10-31
7
431 Views
Last Modified: 2010-04-01
Hi guys,

I need to create a class called "book" that has a private data of :

class book
{
char author[10];
char title[10];
float price;
char publisher[15];
int stock_position;
public :
book()
{
cout<<"enter name:- "cin>>author;
cout<<"enter title:- ";cin>>title;
.
.
.
..
.

 
author
  title
  price
  publisher,and
stock position,and the public fuction of "constructor" using file(in c++) that uses the new operator.and when the user need to search he will enter the title and author name then the system searchs and display if avialable if not it will display an appropriate message.


Please and  Thank you!
0
Comment
Question by:zackexpert
7 Comments
 
LVL 4

Expert Comment

by:hclgroup
ID: 17845016
This sounds like home-work!

Please attempt assignment and ask questions pertaining to where you are having difficulty...
0
 
LVL 8

Expert Comment

by:adg080898
ID: 17845971
What's the question? We can help with your work, but we can't DO your work! What aspect are you having a problem with?
0
 
LVL 4

Accepted Solution

by:
ct.smith earned 125 total points
ID: 17856341
I have one little critique of your posted code.  It's almost always preferable to use fixed point, not floating point, for money values.  With floating point, you end up with rounding errors that shouldn't be there.  For
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 125 total points
ID: 17856741
Some more issues:

>>>> char author[10];

use std::string if possible or at least increase the number of chars to reasonable value, say 32

>>>> char title[10];

Here you should have 128 chars cause some titles might get lengthy.

>>>> book()
>>>> {
>>>> cout<<"enter name:- "cin>>author;

Don't use user I/O in the constructor of a class. I/O should be made in the operative part of a program, i. e. in the main() function or in some operative class, e. g.

class BookShop()
{
      book currentBook;
      std::vector<book> allBooks;
public:
     
      bool addBook();
      // ... more to run a bookshop
};

Put the dialog to the addBook shop and add the new book to the collection of books already stored.

>>>> cout<<"enter name:- "cin>>author;

That is wrong syntax. Put any statement to a new line (for readability reasons)  and close it by a semicolon ;

   cout << "enter name: ";
   cin >> author;

Note, if 'author' isn't a (std) string type but a char[10] array your program writes beyond the array bounds if the user entered more than 9 chars ( + terminating zero char). That's why you should use a suitable big *buffer* if you have to use char arrays (and not std::string what is highly recommended).

   char input[256];   // don't be stingy
   char author[10];  // use the same size as in class book
   ...

   cout << "enter name: ";
   cin >> input;

   int inputlen = strlen(input);
   if (inputlen >= sizeof(author))
         inputlen = sizeof(author)-1;   // cut the input length (or give error message)
   strncpy(author, input, inputlen);   // now is safe
   author[inputlen] = '\0';  // set the terminating 0

   
After getting all attributes you should use a constructor of class book which takes all attributes in the argument list.

class book
{
     ...

public:
     ...
     book(const char a[], const char t[], /* and for all other data members */ );
 
};

Note, you can't simply assign the char array arguments if not using std::string for the members. You have to use strcpy instead.

Regards, Alex
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now