First off, thank you for taking the time to read this.

Here is my question:

I am reading data in from a file that contains names for items that I will be placing onto a heap for use in a program.

The program gives me all of the desired data plus one more piece of data (not desired) with no name.

I can't seem to get my while (!eof) to work properly, and any assistance in this matter would be greatly appreciated.

ifstream dfile; ("batteries.dat");
while (!dfile.eof())
    std::getline(dfile, name, '\n');
    battery.key = 100; = name;

    batteries.insert (battery);
    cout << "Battery " << << " with " << battery.key
         << "% charge"
         << "\nwas added to your shelf." << endl;


Again, thank you for your time


Dont use 'eof' as an exit indicator for the loop. eof() is true only after the end of file is read and not when end of file is reached as a result your getting one extra character. use while(file.getline(<args>)) instead of 'eof'.

Better use

std::ifstream file(...);
std::string line;
while (getline(file,line)) {
if ( { // i/o error occured
This question has already been answered. Start a new discussion instead.