inFile

Hi im trying to figure out the following.

say i inFile a word from a data file.
so like this: inFile >> word;

how can i cout the line where this word is located in the data file?

for example if i have the following data file:

jon 34
bob 45
cat 54
word 66
chaos 34
dog 45

so in the above data file word is in line 4. i want to say
cout << line_word;
and it will tell me which line word is found in the data file.


thanks alot!

Last edited on
You should attempt this on your own first. Then if you get stuck we can help you fix your code.
You can check the file word by word, and compare it to 'word'
You can use std::getline to read a line from the file. Read each line until you find a line that contain the word. You can use std::string::find to check if a line contains the word. You also need to keep track of how many lines you have read so that you can output the line number when the word has been found.
Topic archived. No new replies allowed.