以下是我正在从事的作业的一部分。我被困住了。一点背景:
BookClass是为此任务而构建的类,我知道它可以像以前的任务一样工作。
打开文件并进行第一次读取的工作与应该的一样。循环再次开始后,它将立即跳过整行,并且书的作者姓名将成为书的标题,依此类推.....而将booksInStock保留为空。
我的问题是,是什么导致“ getline(file,bookTitle)”被跳过?
我还附上了一张图片,可能会对详细解释有所帮助。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();
}
正确:
getline(file, bookTitle);
getline(file, authorName);
getline(file, bookPublisher);
getline(file, bookISBN);
file >> bookPrice;
file >> bookYear;
file >> booksInStock;
file.ignore();