How to load vectors with multiple parts

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.

1
2
3
4
5
6
7
8
9
 vector<Book> library;
    openfile(ins);

    string loader;
    while (!ins.eof())
    {
        getline(ins, loader);
        library.push_back(Book());
    }


I also have created this function below that separates a single string into the three parts, but am unsure of how to load the vector using this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Book::Book(string allData)
{
    string holder;
    int hold1 = allData.find("|");
    title = allData.substr(0, hold1);
    int hold2 = allData.find("|", hold1 + 1);
    holder = allData.substr(hold1 + 1, hold2);
    year = atoi(holder.c_str());
    if (year < 0)
    {
        year = 0;
    }
    author = allData.substr(hold2 + 1);
}
What exactly are you inputting into your variables?

Do you have a no-argument constructor declared and defined?

I also have created this function below that separates a single string into the three parts, but am unsure of how to load the vector using this.


You appear to want to load a Book into the vector. If you want to construct a Book using that constructor you must pass a string into the constructor.

By the way you may want to consider using a string stream to parse the string, it may be easier.

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.

I am unsure of how to load it using that constructor. Would it be as simple as doing push_back(Book(loader)); ?
Hello noahk81,

Consider this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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.

Andy
Further consider:

1
2
3
4
while (std::getline(ins, title, '|') && (ins >> year) && std::getline(ins, author))
{
    library.emplace_back(title, year, author);  // <--- Uses an overloaded ctor of the class.
}


So that any file error reading the elements will be correctly detected.
Topic archived. No new replies allowed.