·Word Count· #WSQ12

Hello people! Here is my WSQ12 in which I was told to create a program that could find a certain word (given by the user) in a text file (also given by the user). 

So let’s start with the function. I created a function called find_word and its job was like the name, to find the word in the text file. There are two string variables: the word that the user is looking for and ‘sample’, that is the text file given by the user. The ‘ifstream myfile allows you to look for the text file on your computer. Then I put an if statement that says ‘if the file is open do this…’. As you want to find a word as many times as it appears in the text file, you must analyze each line of the text file, here is where the getline  helps you. This function goes by every line and then with the comparison, you can find the word and the counter increases.

Then you have to close the file with myfile.close() 

Captura de pantalla 2016-03-29 a las 12.09.11 p.m.

And here is the rest of the program, where I ask for the word and the name of the text file. Then I just called my function and that’s it. The result was in the function find_word.

Captura de pantalla 2016-03-29 a las 12.10.37 p.m.

You can look at my code here in GitHub. Feel free to comment.

Thank you Ken for the help with this program!! 

24294566811_7004231734_k
Another Sunrise over the Alps by Rogg4n Link on Flickr: https://www.flickr.com/photos/128294308@N05/24294566811/in/explore-2016-01-14/

CC BY-SA 4.0 ·Word Count· #WSQ12 by alexhfoo is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.