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 6844209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:20:52+00:00 2026-05-27T00:20:52+00:00

This is my first time using EOF and/or files, and I am having an

  • 0

This is my first time using EOF and/or files, and I am having an issue where my code hangs, which I believe is because my EOF is looping one too many times.

I am imputing from a file, and dynamically creating objects that way, and it hangs once the file is run through.

        while( !studentFile.eof() )
    {
        cout << "38\n";
        Student * temp = new Student();
        (*temp).input( studentFile );

        (*sdb).insert( (*temp) );           
    }

This chunk of code is the code in question. The cout >> “38\n”; is the line number and the reason I believe it is hanging from looping one too many times.

The file only contains 4 student’s worth of data, yet 38 appears 5 times, which is the reason I believe it is looping one too many times; Once it gets the last bit of data, it doesn’t seem to register that the file has ended, and loops in again, but there is no data to input so my code hangs.

How do I fix this? Is my logic correct?

Thank you.

  • 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-05-27T00:20:52+00:00Added an answer on May 27, 2026 at 12:20 am

    Others have already pointed out the details of the problem you’ve noticed.

    You should realize, however, that there are more problems you haven’t noticed yet. One is a fairly obvious memory leak. Every iteration of the loop executes: Student * temp = new Student();, but you never execute a matching delete.

    C++ makes memory management much simpler than some other languages (e.g., Java), which require you to new every object you use. In C++, you can just define an object and use it:

    Student temp;
    
    temp.input(studentFile);
    

    This simplifies the code and eliminates the memory leak — your Student object will be automatically destroyed at the end of each iteration, and a (conceptually) new/different one created at the beginning of the next iteration.

    Although it’s not really a bug per se, even that can still be simplified quite a bit. Since whatever sdb points at apparently has an insert member function, you can use it like a standard container (which it may actually be, though it doesn’t really matter either way). To neaten up the code, start by writing an extraction operator for a Student:

    std::istream &operator>>(std::istream &is, Student &s) {
        s.input(is);
        return is;
    }
    

    Then you can just copy data from the stream to the collection:

    std::copy(std::istream_iterator<Student>(studentFile),
              std::istream_iterator<Student>(),
              std::inserter(*sdf));
    

    Note that this automates correct handling of EOF, so you don’t have to deal with problems like you started with at all (even if you wanted to cause them, it wouldn’t be easy).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my first time using nginx and I'm having some problems configuring an
This is my first time using the facebooker plugin with rails, and I'm having
This is my first time using joomla. I don't know if I'm using the
this is my first time using StAX for parsing XML documents (still in the
I'm fairly new to both Django and Python. This is my first time using
This is the first time ever I'm using AJAX, and I want to do
I'm using this XML classes for the first time and can't find this piece
I'm using this line to get the beginning time of the first day of
this is my first time posting here, I have a question which I have
this is my first time using jquery, and i dont think it seems to

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.