I am writing a program for searching for books in a text file, "The library", and I am trying to load the contents of the text file into a vector. This vector uses a class type that I have created with three separate parts. the title, year, and author. All the lines in the text file are in the same format, <Title>|<Year>|<Author>. Is there any way to store each book I can read the text file and load it into the vector while also splitting it up into those separate parts for each book.
Yes, I am trying to load books into the vector using that constructor. Each line in the text file is a book in the format <title>|<year>|<author> and I am trying to load each book into the vector to be able to compare to a search term that is inputted by the user.
But where are you "trying" to load the vector using that constructor? All I see is where you're trying to load the vector using the no-argument constructor.
std::string tital, author;
int year{};
vector<Book> library;
openfile(ins); // <--- I take it that this is a function of yours to open a file stream?
while (getline(ins, title, '|'))
{
ins >> year;
std::getline(ins, author);
library.emplace_back(title, year, author); // <--- Uses an overloaded ctor of the class.
}
Since I do not know what your class looks like a guess would be. An overloaded ctor could be this:
Book (std::string title, int year, std::string author) : m_title(title), m_year(year), m_author(author){}
Your original while loop would create an extra element at the end of the vector and it would be empty or otherwise unusable.