Below is part of an assignment I am working on. I am stuck. A little background:
BookClass is a class built for this assignment, I know it works as it should from a previous assignment.
Opening the file and reading the first time works like it should. Immediately after the loop starts again it skips a complete line and the book authors name becomes the book title and so on.....leaving booksInStock with nothing.
My question is, what is causing "getline(file, bookTitle)" to be skipped?
I also attached an image that might help a bit in explaining it in more detail.Screenshot of example
void getBookInfo(BookClass &book)
{
string bookTitle,
authorName,
bookPublisher,
bookISBN;
double bookPrice;
int bookYear,
booksInStock,
count = 0;
ifstream file;
file.open("books.txt");
while (!file.eof())
{
getline(file, bookTitle);
getline(file, authorName);
getline(file, bookPublisher);
getline(file, bookISBN);
file >> bookPrice;
file >> bookYear;
file >> booksInStock;
book.storeBook(bookTitle, authorName, bookPublisher, bookISBN,
bookPrice, bookYear, booksInStock); //member function of
built class to store info
books[count] = book;
count++;
}
file.close();
}
CORRECT:
getline(file, bookTitle);
getline(file, authorName);
getline(file, bookPublisher);
getline(file, bookISBN);
file >> bookPrice;
file >> bookYear;
file >> booksInStock;
file.ignore();