0

I've been stuck in this and couldn't find a way out of it searching on the internet. This chunk of code supposed to prompt a user for a 3 different string and assign them to member variables of a class.

void Book::setBookProperty()
{
string name;
string publisher;
string category;
int published_year;
double price;

// Prompt for book infromation
cout << "Name of the Book: ";
getline(cin, name, '\n');
this->name = name;

cout << "Publisher: ";
getline(cin, publisher, '\n');
this->publisher = publisher;

cout << "Category: ";
getline(cin, category, '\n');
this->category = category;

cout << "Published year: ";
cin >> published_year;
this->published_year = published_year;

cout << "Price:";
cin >> price;
this->price = price;
}

name, publisher, category, published_year and price are all the private member of the class Book. Assigning those from standard input I'm getting memory access violation!!! which I don understand where am I going wrong. Any help?

And this is how this function is being invoked from another class called BookStore:

void BookStore::addBook(int No)
{
book[No].setBookProperty();
}
5
  • It is probably to do with how this function is called. Can you also show the calling code? Commented Apr 19, 2014 at 0:39
  • This is a lil weird! for the first assignment, everything is OK, means name is being assigned to the member variable NAME of class Book. when it goes to the second assignment! The error pops up. Commented Apr 19, 2014 at 0:41
  • I posted the calling function inside the qustion! Commented Apr 19, 2014 at 0:45
  • 1
    What is BookStore::book defined as and how is it allocated? If book[No] is not a valid Book object then calling setBookProperty() on it would account for the memory errors you are encountering. Please provide a SSCCE demonstrating what you are trying to do. Commented Apr 19, 2014 at 0:48
  • Posted up the definition as an answer below. Commented Apr 19, 2014 at 0:53

2 Answers 2

1

Your book array in BookStore has a length of zero. So you can't fit any books in there. When you call addBook(1), it tries to access book[1] which is past the end of the array, so it is writing to memory it doesn't own.

A quick fix is to set an upper limit to the number of books:

book = new Book[10]; // book store with only 10 books allowed.

A better fix is to store the books in a collection such as std::vector. You can then check if the vector has enough space for book you want to add and add extra room if you need.

vector<Book> book;

void BookStore::addBook(int No)
{
  if (book.size() <= No)
     book.resize(No+1);

  book[No].setBookProperty();
}

Note that both vectors and arrays are zero based, which is why you need to resize to No+1

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I tried the quick fix to limit the number of books allowed! This time another error called assertion failure! Any idea?
No, I'd need some more information - how far through had it got, what the error said, etc. Run it through a debugger and step through the lines, that will help narrow it down.
0

This is how BookStore is defined and an object of Book is being instantiated within it.

class BookStore
{
public:
BookStore(string name)
{
    storeName = name;
    book = new Book[];
}
Book getBook(int);
void addBook(int); // prompts for information
private:
string storeName;
Book *book;
};

And this is how this whole is being called from the main function:

int main()
{
BookStore Amazon("amazon");
Amazon.addBook(1);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.