This page can help you to understand this assignment.
We needed to create a program that asks the user for a word which will be your search word and the name of a file to open and search for that word. Then create a function that will receive two parameters (both string) representing those two data points. This function returns the number of occurrences of that word in that file.
Take a look at this:
First things first. You’ll need these libraries: iostream, string, fstream.
In the int main, you need to declare word, tipo and line as Strings. And the count, you can call it if you want it, but you don’t have to if you don’t wanna”.
And you ask for the word you will look for, and save it into “word”.
And then it asks for the name of the file, save it on tipo.
Well for the find_word() function you declare word and tipo. You equal the count to zero.
The ifstream my file.tipo will look for the file. If the code finds it, the file will open.
The you need to use a while loop (not do-while), with the parameter of getline(myline, line) that will read line by line the file. And if the line equals the word, the count will be count++. This will be ir until the file is done with lines. Then you need to close the file with myfile.close().
In case you couldn’t find it, the else should say “No se encontró” or whatever you want.
The function must return the total count.
Here’s my GitHub link to this code.