Eyram Michael
Eyram Michael

Reputation: 61

Why does this happen in with vectors

I have been writing code to add books to a library.When I add the books to the library and try to display it stores(displays) nothing in the library

Here I set the maximum number of books the library can hold to 100.
int maxbooks=100; vector minilibrary(maxbooks); when I erase the "(maxbooks)" however the code works fine. But I don't understand why?

#include <iostream>
#include <vector> 
#include <string>

using namespace std;

//struct model a book 
    struct book {
    string authorname;
    string bookname;
}; //struct ends here 

//function to display number of books in the library
void displayLibrary(vector <book> &cmini_library,int cnum_of_books);

//function to add books to the library;
void add (vector <book> &cminilibrary,book dummylibrary,int &num_of_books);

int main() {
    int option;
    int curr_num_of_books = 0; //current number of books in the library
    int maxbooks = 100; //max number of books our library can hold 
    vector<book> minilibrary(maxbooks); //minilibrary vector of max capacity 100
    book dummylibrary; // a variable used to hold entries for a short period 

    cout << "1.Display all books in the library" << endl;
    cout << "2.Add a book to the library" << endl;
    cout << "option entered: " << endl;
    cin >> option;

    switch (option) {
        case 2:
            add (minilibrary,dummylibrary,curr_num_of_books);
            displayLibrary(minilibrary,curr_num_of_books);
            break;
        }

    return 0;
}

void displayLibrary(vector <book> &cmini_library,int cnum_of_books)
{
    for(int i=0;i<cnum_of_books;i++) {
        cout << "Book name: \t" << cmini_library[i].bookname << endl;
        cout << "Author: \t" << cmini_library[i].authorname << endl;
    }
}

void add(vector<book> &cmini_library,book dummylibrary, int &num_of_books) 
{
    int booksadded=0; // number of books added to the library
    cout << "How many books do you wish to add: ";
    cin >> booksadded;
    cin.ignore(1,'\n');
    for(int i=0;i<booksadded;i++) {
        cout << "Book name: ";
        getline(cin,dummylibrary.bookname); 
        cout << endl;
        cout << "Author: ";
        getline(cin,dummylibrary.authorname);
        cout << endl;
        cmini_library.push_back(dummylibrary);
    }

    num_of_books = num_of_books + booksadded;
}

Upvotes: 0

Views: 135

Answers (3)

Radosław Cybulski
Radosław Cybulski

Reputation: 2992

Because vector<book> minilibrary(maxbooks); creates minilibrary with 100 elements already inserted. Then you try to display them, but you are using different counter for amount of inserted elements num_of_books. So for example you add one book, now you've 101 books, you print one book (first), which is empty. While the book added is at index 100 (after those 100 initially added).

You probably wanted:

vector<book> minilibrary;
minilibrary.reserve(maxbooks);

EDIT (i'm adding my own comment to increase it's visibility): The whole issue started from failing to obey primary rule: avoid redundancy at all cost. On the one hand you keep your book count as num_of_books variable, on the other hand - minilibrary contains all books. If you skipped num_of_books value and use minilibrary.size(), the problem would be instantly visible - you would get 100 "empty" (uninitialized) books and last book, that you entered.

Upvotes: 2

rafix07
rafix07

Reputation: 20938

int maxbooks=100;
vector <book> minilibrary(maxbooks);

these lines created vector with 100 books, each book was created by default constructor.

Now, when you add new book into vector, its size is 101, but displayLibrary takes 1 as the number of books to be printed, and you print minilibrary[0] - defaulted book, not the book you added i.e. minilibrary[100].

You should create minilibrary as empty vector. Then, you can push books into it.

Upvotes: 5

nimishxotwod
nimishxotwod

Reputation: 335

Vectors are similar to arrays but the difference is they can be dynamically resized. That is why, unlike arrays, you can initialize a vector without specifying its maxlimit (in your case- maxbooks)

Please refer to the documentation on how to initialize a vector in various ways.

Upvotes: 1

Related Questions