Basic Text File Line Counter

void lineNum(string fileName, ifstream& myFile)
{
int lineNum=0;
char ch=0;

while (!myFile.eof())
{
myFile.get (ch);
if(ch=='\n')
lineNum++;
}
cout << "Excellent! The number of lines in " << fileName << "is ---> ";
cout << lineNum << endl;
}

My question is a concern within this function. The program runs, and keeps returning +1 lines than there are in the text file. I noticed within the file, the cursor when at EOF is on a new line without text.
i.e.

giughreuig rgiuhrgh (\n)
ergj rueighre e guhegu (\n)
.....
rvgfrfeff gfuf f gfufuf (\n)
EOF

rather than

giughreuig rgiuhrgh (\n)
ergj rueighre e guhegu (\n)
.....
rvgfrfeff gfuf f gfufuf (\n)EOF


I don't feel right doing a lineNum-- at the end of the code.
Any suggestions???
'\n' means you are going to new line. If you want behavior like in second example, delete newline here: rvgfrfeff gfuf f gfufuf EOF
Topic archived. No new replies allowed.