Question

Inside of main I am reading from a file:sdds::Collection<sdds::Book> library("Bestsellers");

std::ifstream file(argv[1]);

if (!file)

{

std::cerr << "ERROR: Cannot open file [" << argv[1] << "].n";

return 1;

}

std::string strBk;

size_t cnt = 0;

do

{

std::getline(file, strBk);

if (file) {

if (strBk[0] != '#') {

library += Book(strBk);

++cnt;

}

}

} while (file && cnt < 4);





when I try to

std::cout << library;


There is no output.


Here is the += operator of Collection class




Collection<T>& operator+=(const T& item) {

for (size_t i = 0; i < _collSize; i++) {

if (_collArr[i].title() == item.title())

return *this;

}


auto temp_arr = _collArr;


_collArr = new T[_collCurrentItems + 1];

for (size_t i = 0; i < _collCurrentItems; i++) {

 _collArr[i] = temp_arr[i];

}


_collArr[_collCurrentItems] = item;


_collCurrentItems++;



if (_observer)

_observer(*this, item);


delete[] temp_arr;


return *this;

}



and here is the Book constructor


Book::Book(const std::string& strBook) {

constexpr int N = 5;

std::string parsed[N];

char c{ ',' };

size_t startPos = 0, endPos;

for (int i = 0; i < N; i++) { 

endPos = strBook.find(c, startPos);

parsed[i] = strBook.substr(startPos, endPos - startPos);

startPos = endPos + 1;

}


endPos = strBook.length();

std::string temp = strBook.substr(startPos, endPos - startPos);

_description = trim(temp);

_author = trim(parsed[0]);

_title = trim(parsed[1]);

_country = trim(parsed[2]);

temp = trim(parsed[3]);

_price = std::stod(temp);

temp = trim(parsed[4]);

_yrPub = std::stoi(temp);

}


The book does have an assignment operator as well.


The output of library shall be in the following format:


Miguel de Cervantes |      Don Quixote | Spain | 1612 |  9.99 | The story follows a Hidalgo, Alonso Quixano, who proves that chivalry will in fact never die.

   Charles Dickens |  A Tale of Two Cities |  UK | 1859 | 12.32 | A historical novel, set in London and Paris at the begiming and during the French Revolution.

 George R.R. Martin | A Song of Ice and Fire |  US | 1997 | 99.90 | Nine noble famillies begim fighting for control over the mythical lands of Westeros.

   J.R.R. Tolkien | The Lord of the Rings |  UK | 1993 | 21.11 | Sauron has gathered to him all the Rings of Power, and he intends to use them to rule Middle-earth.

    J.K. Rowling |      Harry Potter |  UK | 1997 | 45.99 | Harry realizes his life is far from ordinary.

      Dan Brown |   The Da Vinci Code |  US | 2003 |  7.88 | While in Paris, Harvard symbologist Robert Langdon is awakened by a phone call in the night.

    J.D. Salinger | The Catcher in the Rye |  US | 1951 | 12.21 | The story of a teen named Holden Caulfield and his struggle to find his voice in an adult world.


What am I doing wrong? the operator << of both Book and Collection is correct

Answer & Explanation

Unlock full access to Course Hero

Explore over 16 million step-by-step answers from our library

Get answer

Our verified expert tutors typically answer within 15-30 minutes.