Thread: stopping at eof

  1. #1
    Registered User
    Join Date
    Jan 2002

    stopping at eof

    The program I am trying to write has to stop reading data from a file at 50 names or at EOF, whichever comes first. The test data file that I'm using has 2 pieces of data in it so it should stop at EOF. The problem is, is that is it is creating 50 entries in the output files that contain blank data. Please help. Here is the code that contains the lines for the loop. Thanks.

    cout << "Please enter the name of the input file.";
    cout << "Please enter the name of the output file.";
    outfile << "Program 2 The people class:" << '\n' << '\n';
    while(i<50 || !EOF)

    outfile << "The name is: " << name << '\n';

    infile >> num;
    if (num>0 && num<100)
    num2 = num2+num;

  2. #2
    while(i<50 || !EOF)

    !EOF evaluates to 0 every time

    you are doing: while(i < 50 || 0)

  3. #3
    use two conditionals in the while loop.

    while(i<50 && !infile.eof())

    or whatever other method for detecting EOF you wish to use.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. EOF Explanation Anybody?
    By blackcell in forum C Programming
    Replies: 1
    Last Post: 01-29-2008, 09:09 PM
  2. EOF or not EOF?
    By CornedBee in forum Linux Programming
    Replies: 2
    Last Post: 09-14-2007, 02:25 PM
  3. EOF messing up my input stream?
    By Decrypt in forum C++ Programming
    Replies: 4
    Last Post: 09-30-2005, 03:00 PM
  4. whats the deal with EOF really ???
    By gemini_shooter in forum C Programming
    Replies: 7
    Last Post: 03-06-2005, 04:04 PM
  5. files won't stop being read!!!
    By jverkoey in forum C++ Programming
    Replies: 15
    Last Post: 04-10-2003, 05:28 AM