Last Record Repeats Why?

I have made this program -
File data_n.txt has data-
1 1 1
2 4 8
3 9 27
4 16 64

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
ifstream ifile ("data_n.txt");
int n1, n2, n3;
int c = 1;

while ( !ifile.eof() ) {
ifile >> n1 >> n2 >> n3;
if ( n1 != c ) {
cout << "Line No. " << c << " is missing..." << endl;
c++;
}
c++;
}

ifile.close();

return 0;
}

on executing this program, it is repeating last line 2 times. so i am getting wrong answer. Why is it happening and what is its solution?
Check wether data_n.txt is like this:
1 1 1
2 4 8
3 9 27
4 16 64

or like this:
1 1 1
2 4 8
3 9 27
4 16 64

An extra line could do that
You need to check the stream after reading n1, n2, and n3.

Given the file:

1
2
3
4
1 1 1
2 4 8
3 9 27
4 16 64


(ie, without the blank line at the end),

after you read and process the last line, ifile.eof() will still return false even though there is no more data left to read.

so

1
2
3
4
5
6
while( !ifile.eof() ) {
    ifile >> n1 >> n2 >> n3;
    if( ifile.eof() )
        break;
    // ... process
}

jsmith is wright, but try also with this input file:
1 1 1
2 4 8
5 9 27
6 16 64

It will tell u that
" line no. 3 is missing
line n0.5 is missing".
U don't need to increase c after cout<<"line..."; because it will also increase outside the if bloc.

Something like this is enough:

while ( !ifile.eof() ) {
ifile >> n1 >> n2 >> n3;
if( ifile.eof() )
break;

if ( n1 != c )
cout << "Line No. " << c << " is missing..." << endl;
c++;
}

thanks jsmith!!!!!!
Topic archived. No new replies allowed.