So example.txt contains this;
The
Man
Walked
Into
The
Forest
What I would like is for it to show, what line the word "Walked" is on.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
string line;
string word = "Walked";
ifstream myfile ("example.txt");
if (myfile.is_open())
{
while ( myfile.good() )
{
getline (myfile,line);
if(line == word) {
// Walked found on line 3
} else {
cout << line << endl;
}
}
myfile.close();
}
else cout << "Unable to open file";
system("pause");
return 0;
}
So, the output of the code would be "The word walked, is on line 3."