Thread: stopping at eof

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    9

    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.";
    gets(input);
    infile.open(input);
    if(infile)
    {
    cout << "Please enter the name of the output file.";
    gets(output);
    outfile.open(output);
    if(outfile)
    {
    outfile << "Program 2 The people class:" << '\n' << '\n';
    while(i<50 || !EOF)
    {

    getline(infile,name);
    outfile << "The name is: " << name << '\n';

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

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

    !EOF evaluates to 0 every time

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

  3. #3
    Unregistered
    Guest
    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