Solved

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

Posted on 2006-10-31
7
452 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

752 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