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();
}
BookStore::bookdefined as and how is it allocated? Ifbook[No]is not a validBookobject then callingsetBookProperty()on it would account for the memory errors you are encountering. Please provide a SSCCE demonstrating what you are trying to do.