How to store values into vectors/arrays?

For my assignment I have to create a library using text files as our input source of data. The data from the text files look like this with several entries
11 Walter_Savitch Computer_Science 3
Starts with the ID followed by the title, author, category and then copies.
My program so far is able to read all the lines but how do I store the values into an array or a vector. For example, I store the title of the 1st line into a vector so that if a person wanted to add a new book and the title is the same it will increase the amount of copies for that book by 1. If you have any other ideas on how to do this it will be greatly appreciated!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
       void Library::AddBook()
{
     fstream infile;
     string line;
     infile.open ("books.txt");
     if (!infile)
     {
                 cout<<"error opening file"<<endl;
                 exit (1);
     }
    
     while (infile)
     {
     infile>>ID>>title>>author>>category>>copies;
     cout<<ID<<title<<author<<category<<copies<<endl;
     }
     cout<<"Enter the title of the book you wish to add use _ instead of"
     <<" spaces."<<endl;
     cin>>title;


This is the snippet.
Have you covered structs yet?
You didn't post the declaration for your Library class, so I don't know what you have there.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
struct Book
{  int ID;
   string title;
   string authod;
   string category;
   int copies;
};

//  Declare an instance of Book and a vector of books
  Book book;
  vector<Book>  books;

//  Replacing lines 14-15
  infile>>book.ID>>book.title>>book.author>>book.category>>book.copies; 
  cout<<book.ID<<book.title<<book.author<<book.category<<book.copies<<endl;
books.push_back (book);


Note that this does not check if the book is already in the vector.
Last edited on
Create a class which holds all information of the book. Create a vector of said books:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
struct Book
{
    unsigned ID;
    std::string title;
    std::string author;
    std::string category;
    unsigned copies; //?
};

//...
std::vector<Book> books;
Book book;
//Loop on input or check input state after reading
while(infile >> book.ID >> book.title >> book.author >> book.category >> book.copies)
    books.push_back(book);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

// some sort of structure for your book

struct book
{
    int id;
    std::string title;
    std::string author;
    std::string category;
    int copies;
}

// a vector of books to store your data in
std::vector<book> library;

// create a book instance and copy the data 
book temp;
temp.id = ID;
temp.title = title;
temp.author = author;
temp.category = category;
temp.copies = copies;

// store your book in a vector
library.push_back(temp);
This was my class, sorry for not putting it up earlier. Everything seems fine so far except I am clueless on to how to check if the vector has the book
This is the class
1
2
3
4
5
6
7
8
class Books
{
      public:
             int ID, Copies;
             string Title, Author, Category;
      void addbook();
      void modifybook();
};


It gives me errors every time I try searching the vector.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
void Books::addbook()
{
   Books Book;
   string s;
   vector<Books> library;
   fstream infile;
   infile.open("books.txt");
   if (!infile)
   {
        cout<<"error opening file"<<endl;
   }
   while (infile>>Book.ID>>Book.Title>>Book.Author>>Book.Category>>Book.Copies)
   {
           
       cout<<Book.ID<<Book.Title<<Book.Author<<Book.Category<<Book.Copies<<endl;
       library.push_back(Book);
   }

//This is where the error happens, before this its all fine
  bool isPresent = (std::find(library.begin(), library.end(), "Absolute_C++") != library.end());
  return s=="Absolute_C++";


    
};



std::find(library.begin(), library.end(), "Absolute_C++")
And what this code should do? Should it search it in Title? In author? In category? ID, Copies?

YOu need to either create custom comparsion function/functor, or use lambdas:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
struct TitleComparer
{
    TitleComparer(std::string title_) : title(std::move(title_)) {}
    bool operator()(const Book& b)
    { return b == title; }
private;
    std::string title;
}
//...
std::find_if(library.begin(), library.end(), TitleComparer("Absolute_C++"));

//OR

std::find_if(library.begin(), library.end(), [](const Book& b) 
                 { return b == "Absolute_C++"; });
A couple of obvious problems:

Line 20: You set isPresent, but you don't do anything with it.

Line 21: You're attempting to return a bool result, but Books::addBook() is a void function. Also, s is never set to anyting, so line 21 will always evaluate to false.

You have a more basic structural problem here also. vector<Books> library is a local variable that goes out of scope when Books::addBook exits.

If you look at your class members, your class represents one book.
addbook() is an action you perform on a library object, not on a book itself.
i.e. You add a book to a library, you don't add a book to itself.
Last edited on
Wow, I didn't realize that till now. My class is supposed to represent multiple books, the file has about 5 different book entries and I need to be able to store data from all 5 and be able to edit them, and add as many books as needed any suggestions on how I can make this more efficient?
My class is supposed to represent multiple books


Classes are supposed to model what they represent. You're just going to create problems for yourself by trying to have your Books class represent both a single book and a collection of books.

I would suggest making a Library class. Your Library class should contain the vector<Books>. You then want to load the vector from your file. I would also suggest renaming your Books class to just Book, since it represents a single book.

This should give you a start.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Library 
{  vector<Book>   m_books;
public: 
    void LoadFromFile (const string & filename)
    {  //  Read file into vector
    }
    void AddBook (const Book & book)
    { //  Need to check for book already in vector 
       m_books.push_back (book);
    }
    void DisplayBooks ()
    {  //  Iterate through the vector pointing to one book at a time
        iter->DisplayBook ();
    }
    const Book * FindBook (int id)
    {  // Search books for match on ID, return pointer to book 
        // or NULL if not found
    }
};


If you don't want to create a Library class, you've already had three nearly identical suggestions earlier in this thread about creating a struct for a book and a vector<Book>.
Thanks for the help! I finished my assignment and got a good grade on it.
Topic archived. No new replies allowed.