Book book;
list<Book>* books;
string title;
string author;
int ISBN;
Book* Administrator::addBook()
{
Book *newBook = new Book();
cout << "Would you like to enter a book?" << endl;
cin >> userInput;
cout << endl;
if (userInput == "yes")
{
cout << "What is the title of the book you want to enter?" << endl;
cin >> title;
cout << "What is the author of the book you want to enter?" << endl;
cin >> author;
cout << "What is the ISBN of the book you want to enter?" << endl;
cin >> ISBN;
cout << endl;
newBook->setTitle(title);
newBook->setAuthor(author);
newBook->setISBN(ISBN);
newBook->setAvailability(true);
books->push_back(*newBook);
}
return newBook;
}
Here im creating my book objects in my Administration class, the problem im having is when i try and access them from another class, its says they are not there.
I did a bit of reading and understand I've to allocate the objects to the heap using dynamic memory management, is there a way i can do this within my code?
Any help would greatly be appreciated.
Here's a simple way:
if (userInput == "yes")
{
Book *newBook = new Book(); // <-- allocates a Book object on the heap
cout << "What is the title of the book you want to enter?" << endl;
[...]
newBook->setTitle(title);
[...]
return *newBook;
}
you have to decide if you want to use the book that is in your class, or create a different book, or a heap copy of your book
if you want a different book you will need to use the new operator at the start
if you want to create a copy of your book on the heap:
return new Book(book)
Book* Administrator::addBook()
{
Book *bookOnHeap = new Book();
....
return bookOnHeap;
}
Related
I'm writing a basic program, similar to ELIZA(online therapist) simple questions and answers but I'm stuck at the end. after cin >> answer; I'm not able to write anything.
int main () {
short number;
string color;
string sport;
int answer;
string travel;
// Greets user
cout << "Hello, I'm Samantha" << endl;
// Asks user for their favorite sport
cout << "What's your favorite sport?";
cin >> sport;
cout << "I like " << sport << " too!" << endl;
cout << "How about your favorite color?";
cin >> color;
cout << "Not my favorite color but it's nice!" << endl;
cout << "Tell me something you've never told anyone before";
cin >> answer;
cout << "Don't worry, your secret is safe with me!" << endl;
cout << "Hows your life going?";
cin >> answer;
return 0;
}
your variable named "answer" has a data type integer. the first time you prompt the user to enter something from the console (apparently to be a string) the cin object attempts to initialize "answer" with a string (probably because the prompts does not ask for a number) which kills the cin object which will not allow the object to take instructions...so the next time you want to use it there is no cin object.
Just change the data type for "answer" to string.
Dr t
This question already has answers here:
What's the difference between passing by reference vs. passing by value?
(18 answers)
Closed 7 years ago.
I'm writing a bookshop program. The program stores a number of books with their associated values (title, price, isbn, author) in a vector. One of the functions I'm trying to write is to search the vector by isbn and update the values of whichever book matches. Here is my code
void UpdateOnIsbn(vector <CBooks> booklist)
{
string searchisbn;
char response;
string booktitle;
string author;
double price;
string ISBN;
cout << "Please enter an ISBN to be searched: ";
cin >> searchisbn;
for (int i = 0; i < booklist.size(); i++)
{
if (booklist[i].HasISBN(searchisbn))
{
booklist[i].Display();
cout << "Would you like to update the details of this book? (Y/N): ";
cin >> response;
if (response != 'n' && response != 'N')
{
cout << endl << "Please Enter New Title for book: ";
cin >> booktitle;
booklist[i].SetTitle(booktitle);
cout << endl << "Please Enter New Author ";
cin >> author;
booklist[i].SetAuthor(author);
cout << endl << "Please Enter New Price ";
cin >> price;
booklist[i].SetPrice(price);
cout << endl << "Please Enter New ISBN ";
cin >> ISBN;
booklist[i].SetISBN(ISBN);
}
}
}
}
The function seems to work as it looks for new values to be entered but after it runs the old values are not replaced when I display the books again. Please help
Here is an example of one of the set functions:
void CBooks::SetPrice(double NewPrice)
{
m_Price = NewPrice;
}
You are passing a copy of booklist so you are modifying the copy not the original object.
Try passing a reference to the function void UpdateOnIsbn(vector <CBooks>& booklist)
You need to pass booklist by reference:
void UpdateOnIsbn(vector <CBooks>& booklist)
Otherwise the vector is copied and only this copy is modified.
list<Book> *books = new list<Book>;
list<Book>::iterator pos;
void Administrator::addBook()
{
Book *newBook = new Book();
cout << "Would you like to enter a book?" << endl;
cin >> userInput;
cout << endl;
if (userInput == "yes")
{
cout << "What is the title of the book you want to enter?" << endl;
cin >> title;
cout << "What is the author of the book you want to enter?" << endl;
cin >> author;
cout << "What is the ISBN of the book you want to enter?" << endl;
cin >> ISBN;
cout << endl;
newBook->setTitle(title);
newBook->setAuthor(author);
newBook->setISBN(ISBN);
newBook->setAvailability(true);
books->push_back(*newBook);
}
}
*****************
I really need help with this, i have received a few answers today but none have helped me with this problem. This is my admin class. It creates books on the heap and stores them in an stl::list
void Guest::searchBook(Book* search)
{
string searchBook;
cout << "What book would you like to search for?" << endl;
cin >> searchBook;
printBookDetails();
}
This is my Guest class, what i would like to do here is to search through the list of books i created in my Administrator class but when it goes into the function printBookDetails, my list contains no elements, im assuming they've been destroyed.
Administrator* admin1 = new Administrator("jayfitz91", 24681357);
Guest* guest1 = new Guest("guest", 0000);
void main()
{
//Everything here works fine
admin1->addBook();
admin1->addBook();
admin1->makeAvailable();
admin1->printBookDetails();
//My list is destroyed at this point and it returns nothing
guest1->printBookDetails();
My Guest class inherits the printBookDetails from my Administrator class
All of the admin functions work but as soon as it gets to guest, the elements disappear.
Is there anyway i can get around this? The help would greatly be appreciated
Thanks to #OldProgrammer, i realised i had to return the list of books from my Admin class and pass it through as a parameter to my printBookDetails method in my guest class:
//Admin class
//Now returning a list
list<Book> Administrator::addBook()
{
Book *newBook = new Book();
cout << "Would you like to enter a book?" << endl;
cin >> userInput;
cout << endl;
if (userInput == "yes")
{
cout << "What is the title of the book you want to enter?" << endl;
cin >> title;
cout << "What is the author of the book you want to enter?" << endl;
cin >> author;
cout << "What is the ISBN of the book you want to enter?" << endl;
cin >> ISBN;
cout << endl;
newBook->setTitle(title);
newBook->setAuthor(author);
newBook->setISBN(ISBN);
newBook->setAvailability(true);
books->push_back(*newBook);
}
return *books;
}
//Guest class function
void Guest::printBookList(list<Book> *tempList)
{
pos = tempList->begin();
for (pos = tempList->begin(); pos != tempList->end(); ++pos)
{
cout << pos->getTitle() << "\n"
<< pos->getAuthor() << "\n"
<< pos->getISBN() << "\n"
<< pos->getAvailability() << "\n"
<< "******************************" << endl;
}
}
//Main class function
//Create a temporary variable for the list
list<Book> tempList;
//pass it through to the method
guest1->printBookList(&tempList);
First off, you don't have to dynamically create your list object.
list<Book> books = new list<Book>();
You do this because
you have less responsibility: you don't have to release the resources contained by books. It will automatically be released.
list is a stl container, which means it automatically handles dynamic allocations.
This is different if you use an dynamically allocated array...
I think your list is "gone" is because Guest and Administrator has two entirely different list.
To solve this issue, you should grant access to the book list that Administrator has to your Guest.
Or extract out the book list and store it in another class that allow access to the list to both Administrator and Guest.
list<Book*> books;
list<Book>::iterator pos, last;
Book Administrator::addBook()
{
Book *newBook = new Book();
cout << "Would you like to enter a book?" << endl;
cin >> userInput;
cout << endl;
if (userInput == "yes")
{
cout << "What is the title of the book you want to enter?" << endl;
cin >> title;
cout << "What is the author of the book you want to enter?" << endl;
cin >> author;
cout << "What is the ISBN of the book you want to enter?" << endl;
cin >> ISBN;
cout << endl;
newBook->setTitle(title);
newBook->setAuthor(author);
newBook->setISBN(ISBN);
newBook->setAvailability(true);
books.push_back(newBook);
}
return *newBook;
}
void Administrator::printBookDetails()
{
books.begin()->setPrevBook(NULL);
for (pos = books.begin(); pos != books.end(); ++pos)
{
cout << pos->getTitle() << "\n"
<< pos->getAuthor() << "\n"
<< pos->getISBN() << "\n"
<< pos->getAvailability() << "\n"
<< "******************************" << endl;
if (pos != books.begin())
{
last->setNextBook(&*pos);
pos->setPrevBook(&*last);
}
last = pos;
}
books.back().setNextBook(NULL);
}
Can someone help me finish off this project please, these are my two functions addBook and printbookDetails . These are in my Admin class.
I would like the books i create on the heap to be stored in list< book*> books as i want to reference them in another class.
I've gotten a bit of help on here so far in regards to pointers and i know its something to do with me not linking the pointers to the correct objects.
My printBookDetails is giving me trouble, the first line books.begin()->setPrevBook(NULL); is saying i need a pointer-to-class type but when i put the -> i still have an error.
Book Guest::searchBook(Book* search)
{
string searchBook;
cout << "What book would you like to search for?" << endl;
cin >> searchBook;
printBookDetails();
}
What id like to do is use the searchBook function above in my Guest class to reference the books in the list at a later time but when i cannot seem to get my pointers right.
Can someone please put me on the right track.
for (pos = books.begin(); pos != books.end(); ++pos)
Here, pos is an iterator, over your list. Iterators are like pointers to list entries.
But each of your list entries is also a pointer to a book, not a book itself.
So, you need code like: (*pos)->getTitle() when trying to access member functions for those elements.
Book Administrator::bookDetails()
{
Book book;
list<Book> books;
string title;
string author;
int ISBN;
string userInput;
while (userInput != "q")
{
cout << "Would you like to enter a book?" << endl;
cin >> userInput;
if (userInput == "yes")
{
cout << "What is the title of the book you want to enter?" << endl;
cin >> title;
cout << "What is the author of the book you want to enter?" << endl;
cin >> author;
cout << "What is the ISBN of the book you want to enter?" << endl;
cin >> ISBN;
book.setTitle(title);
book.setAuthor(author);
book.setISBN(ISBN);
books.push_back(book);
}
list<Book>::iterator pos;
pos = books.begin();
for (pos = books.begin(); pos != books.end(); pos++)
{
//There error is produced here
cout << *pos << "\n";
}
}
return book;
}
This is the bookDetails function of my Administration class. It loops through and asks for a books title, author and ISBN number and when its finished it pushes the book onto a list.
This seems to work fine when i debug it but i get an error when i try to print out the details of each book using the iterator.
Can anyone help me out here?
Thanks
You need to implement operator<< for Book
std::ostream operator<<(std::ostream& os, const Book& book)
{
os << book.title << " " << book.author; // print out other information
return os;
}