HELP! Avoiding duplicate enteries.

I'm having an assignment where I have to make a book library in C++ using several classes through an inheritance heirarchy as well as composition. I'm stuck at one place where it asks that each book should have a unique ISBN and that if I enter an ISBN that already exists, it should produce an error. Can someone tell me how to put that check in a class that it doesn't allow a data member to have the same value.

Regards.
Last edited on
can you use a std::map for this?
I don't think so as we haven't studied about that yet :(
Last edited on
How are you planning to keep the book list in your class? whats the class look like?
All you need is a simple loop here. Iterate through the container you use and check ISBN of each book in it.
I think it needs an if condition where I take an input for adding books to the library.

If (ISBN == ? )

I dont understand what should come in place of that question mark, if for example i'm taking the input in the book class' constructor.
ok. so you have a book class which will have a name and a ISBN number.
Now you will be having a library which will have a list of books. So something like:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class book
{
name;
isbn ;

};

class library
{
private:

book  m_list_of_books;

public:
bool AddBook(name, isbn);

};




Now the question is, how will you keep the list of books inside the library?

if you can answer this question, then you have to search that list for the isbn number. If you find that number your AddBook() function will return false and will not add thebook else the book will be added to the m_list_of_books.

So you will do:

1
2
3
4
if(!library::AddBook(isbn))
print "Error: already added";
else
print "Success: book added to library";

Book class declaration

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
using namespace std;

#ifndef BOOK_H
#define BOOK_H

class Book
{
private:
	char title[20];
	char author[15];
	char type[10];

public:
	Book();

	Book(char [], char [], char []);

	void AddBook(char [], char [], char []);

	virtual void print();

	virtual ~Book();
};

#endif 


Definition:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <cstring>
using namespace std;

#include "book.h"

Book::Book(){}

Book::Book(char a[], char b[], char c[])
{
	strcpy(title, a);
	strcpy(author, b);
	strcpy(type, c);
}

void Book::AddBook(char a[], char b[], char c[])
{
	strcpy(title, a);
	strcpy(author, b);
	strcpy(type, c);
}

void Book::print()
{
	cout<<"Book: "<<title<<" by "<<author<<" of type "<<type<<endl;
}

Book::~Book(){}


This is the book class. Need to put the check on the data member "title".
Last edited on
Are there any alternate solutions to the one than what writetonsharma has posted?
writetonsharma didn't give you a solution but rather explained how you should put the solution in your code.
It is a good idea to have AddBook return a boolean to let user know whether the book was added or not.
You need a function
1
2
3
4
bool Book::find( const char* name ) {
   for( int i = 0; i < 20; i++ ) if( strcmp( title[i], name ) != 0 ) return true;
   return true;
}

and then in your AddBook write first line if( find(a) ) return false; (or just return if you choose AddBook not to return anything, of course).
Last edited on

comments:

1. dont use char*, instead use std::string. You are programming in c++.
eg:

1
2
3
4
5
6
7
8
private:

std::string m_title;

Book::Book(const std::string& title)
: m_title(title)
{	
}


try it, its simple and you will actually prefer it.

2. you will not put the check in the book class. As book class represent a single book and not a list of books.
make a library class the same way.

class library
{
private:

std::list<book> m_BookList;


};


to add anything to thelist, just do this:

1
2
Book b("title");
m_BookList.push_back(b);
Last edited on
I cannot edit.

Now when you try to push in the list, you have to use algorithm:

1
2
3
4
5
6
Book b("title");
if(std::find(BookList.begin(), BookList.end(), b) == BookList.end())
//book not found,
m_BookList.push_back(b); 
else
//book found, dont add it. 


does this makes sense?

Edit:
reply clashed with hamsterman.
Topic archived. No new replies allowed.