Possible Duplicate:
Why is iostream::eof inside a loop condition considered wrong?
im having a problem with the eof() function. my loop is not reading the end of the file that im reading from thus leaving me with an infinite loop. Any help or insight would be greatly appreciated. thanks
while (!file2.eof()) {
getline (file2, title, ',');
getline (file2, authorf, ',');
getline (file2, authorl, ',');
getline (file2, isbn, ',');
file2 >> pages;
file2.ignore();
file2 >> price;
file2.ignore();
getline(file2, subject, ',');
file2 >> code;
file1.ignore();
file2 >> rentalp;
file2.ignore(10, '\n');
textbook b2(title, authorf, authorl, publisher, pages, isbn, price, code, subject, rentalp);
b2.PrintTbook();
TbookList[j] = b2; //initalizing the first element of the array to b2.
newFile << "Title: " << TbookList[j].getTitle() << "\n" << "Price: " << TbookList[j].getPrice() << "\n\n";
TbookList[j].PrintBook();
j++;
textbookCount++;
}
the text file looks like this:
A Practical Introduction to Data Structures and Algorithim Analysis, Clifford, Shaffer, 0-13-028446-7, 512, 90.00, Computer Science, E, 12.00, 2001 Fundamentals of Database Systems, Ramez, AlMasri, 9-780805-317558, 955, 115.50, Computer Science, E, 0.0, 2003
First of all, almost any loop of the form
while (!whatever.eof())
is completely broken.Second, you have what I'm going to assume is a typo:
The rest of the code is reading from
file2
, so I'm going to guessfile1
is just a typo here (but if you've copied it correctly, it could be the real source of a problem).You usually want to do things like this by overloading
operator>>
for the type you're reading:Then you can read in a bunch of objects something like: