i am making a library database which asks users to enter the book title, the number of authors and the names of those authors and this is the header file i came up with..
#include <iostream>
#include <string>
usingnamespace std;
{
class Library
public:
Library(); //Creates an empty library without books and without authors.
~Library(); //Destructor
void add(string title, string authors[], int nAuthors); // Add a book
void print() const; // Print the list of all books ordered by book title.
void printByAuthor(string author) const; //Print the list of books of a given author, ordered by book title.
private:
struct book
{
string title;
string *author;
int nAuthors;
book *link;
};
};
i am now writing the implementation file and i have so far come up with this
#include "library.h"
#include <iostream>
#include <string>
#include <cassert>
usingnamespace std;
Library::Library()
{
first = NULL;
last = NULL;
count = 0;
}
Library::~Library()
{
nodeType *temp;
while (first != NULL)
{
temp = first;
first = first->link;
delete temp;
}
last = NULL;
count = 0;
}
void Library::add(string title, string authors[], int nAuthors)
{
if (library == NULL)
{
}
void Library::print() const
{
book *link;
link = first;
while (link != NULL)
{
cout << link->info->title << " ";
link = link->link;
}
}
void Library::printByAuthor(string author) const
{
}
i am having trouble with my add function as it has to use *author to point it to the dynamic array of authors that is to be entered.can any one help me
It's difficult to answer you because you've missed some of the code. For example I assume library is the head of your linked list of struct book. I would suggest not doing a linked list all, try using one of the STL containers; maybe a vector or a double ended queue (deque). I would also use a vector of strings rather than string array. These containers provide all the mechanisms for managing the list of items without you having to write them and they are very easy to use For example
#include <vector>
#include <string>
class myclass
{
public:
myclass(void){}
~myclass(void)
{
mystrings_.clear();
}
addString(const std::string str)
{
// Add a string to the container
mystrings.push_back(str);
}
int getNumStrings(void)
{
// return the number of strings in the container
return mystrings.size();
}
std::string getAString(int index_num)
{
// Either check not out of bounds like this
// or handle exceptions thrown by vector
if (index_num < mystrings.size())
{
return mystrings[index_num];
}
}
private:
std:vector<std::string>mystrings_;
};
the STL vector object can be created to contain any structure or pointer (be careful calling clear or erase etc if it has pointers, remember to delete first) . There are lots of tutorials and examples about on vectors and deques. If you still want to do a linked list, I don't understand why, but you will need to write a lot more code to do the same thing. Bear in mind that lots of developers have spent a lot of time fine tuning the code for these STL containers for speed, efficiency and reliability. The chances of someone writing for and while loops to match it, never mind beat it are slim to zero. It is just not worth the effort.
sorry 2 ruin all ur good work but i have to use that header file..my teacher stated that i have to use that one and im not allowed to use stl, it has to be pointer structures(dynamic arrays, linked lists)...tough break ay
Yep. Not many reasons for writing linked lists in C++, that'll be one of them.
So, linked lists. I'm not sure what nodeType and library (lower case el) are, or info (in your print()) so I've written it with the information from your header file.
I have assumed first and last are struct book pointers and I've included an example of print so you can see how to access the string * objects.
void Library::add(string title, string authors[], int nAuthors)
{
// need a copy of struct book that won't go out of scope to put in linked list
struct book * temp = new book;
int i;
temp->title = title;
// ...and a copy of the string array for the same reason
temp->author = new string[nAuthors];
temp->nAuthors = nAuthors;
for (i = 0; i < nAuthors; i++)
{
temp->author[i] = authors[i];
}
// initiialse link to NULL
temp->link = NULL;
// Make sure last always points to last element of list
if (first == NULL)
{
first = temp;
last = first;
}
else
{
last->link = temp;
last = temp;
}
}
void Library::print() const
{
struct book *temp;
int i;
temp = first;
while (temp != NULL)
{
cout << temp->title << ": ";
for (i = 0; i < temp->nAuthors; i++)
{
cout << temp->author[i];
if (i < temp->nAuthors - 1)
{
cout << ", ";
}
}
cout << endl;
temp = temp->link;
}
}
You will need to add a delete[] in your destructor to get rid of the string array in each struct book. One last thing, in your header you have the opening brace before the class Library statement, it needs to be after.
Though you are right that many experienced developers put effort for fine tuning the STLs for time, speed and efficiency, you are forgetting one thing, I would not say that using or writing a linked list is a waste of time.
Please dont assume all developers use only STL and attempt to write for a waste of time.
Linked lists are used still in many development projects as the requirements and necessity vary.
Vectors or any STL containers are always not chosen to be best solution.
And coming to the poster's request, please note that he is a student and learning C++ which helps you to write great programs with all his/your brainwork and the postar (shotjase) wants to be such brain.
Hence we better help him the way he wants.
C++ is not meant to be like Java which is all most of built-in objects and lets you use it handy. When it fails you dont know what has gone wrong, like MicroSoft wizard driven programming.
C++ gives you a lot of scope to write your own code as you like it (not with many objects already built-in) and develop it with your own brainwork, one prime reason I like most.
Let us have fun all with such development, but not with handy readymade objects.
@satm2008:
You have a very good point. However, less experienced developers quite often don't know what is available and in this example, could quite easily have been attempting one of the more difficult techniques when there were easier-to-implement alternatives. It was a more a matter of making sure the person posting the the question was aware of the alternatives and an understanding of the pros and cons of making the choice. As it turned out they didn't have a choice, but I do accept that I could have taken a more balanced approach to guidance rather than concentrating on the STL solution.