Solved

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

Posted on 2006-10-31
7
447 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

685 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