Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8651579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:06:24+00:00 2026-06-12T14:06:24+00:00

Possible Duplicate: Why is iostream::eof inside a loop condition considered wrong? im having a

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T14:06:26+00:00Added an answer on June 12, 2026 at 2:06 pm

    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:

    file1.ignore();
    

    The rest of the code is reading from file2, so I’m going to guess file1 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:

    std::istream &operator>>(std::istream &is, textbook &b2) {
        getline (is, title, ','); 
        getline (is, authorf, ',');
        getline (is, authorl, ',');
        getline (is, isbn, ',');
        is>> pages;
        is.ignore();
        is>> price;
        is.ignore();
        getline(is, subject, ',');
        is>> code;
        is.ignore();
        is>> rentalp;
        is.ignore(10, '\n');
        return is;
    }
    

    Then you can read in a bunch of objects something like:

    std::vector<textbook> books;
    
    textbook temp;
    
    while (file2>>temp) {
        books.push_back(temp);
        temp.printbook();
        // ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Why is iostream::eof inside a loop condition considered wrong? So I've been
Possible Duplicate: .eof() loop not working So I have a fairly simple C++ program
Possible Duplicate: Copy Constructor is not invoked # include <iostream> using namespace std; class
Possible Duplicate: Can main function call itself in C++? I found this problem very
Possible Duplicate: Parentheses at the end of a C++11 lambda expression #include <iostream> int
Possible Duplicate: Program not waiting for cin I wrote the following code: #include <iostream>
Possible Duplicate: Floating point inaccuracy examples Im having a problem... When i compile the
Possible Duplicate: Reading from text file until EOF repeats last line c++ EOF running
Possible Duplicate: How is std::iostream buffered? This might sound ridiculous, but how can I
Possible Duplicate: No such file iostream.h when including Even after naming the source file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.