You may also want to check that you actually read as many lines as the loop went. So to check to see if you have reached EOF or not, change line 17 to read:
1 2
if (!f.eof()) cout<<"The fourth line is:\n" << line << endl;
else cout<<"There aren't that many lines in the file.\n";
Thank Duoas. Your code is much better than mine. I had looked at the reference files on getline but didn't know how to make it work with ifstream. I was just happy to have a something that could worked for this one program.