What is the best method to get the frequency of words in a file? I have this so far, however it only grabs the first word in the file and checks it with itself.
Grab the word in a temp string. Compare it against the words you've already grabbed and stored into a vector. If it's not found push_back() that word into the vector. Repeat.
EDIT: Of course you'll actually want to use a custom class or struct to store the variables so that you can associate an int with the word to count how many times it occured.
EDIT 2: Did you want psuedo code? I don't want to just give you everything because some people enjoy a challenge.
Few things- I understand most of what you did, other than the concept of the class. Is there a way to do it with a function such a void(stuff) rather than a class? Haven't touched classes yet in what I am doing. Also why on line 32 do you not check for eof?
- Objects aren't terribly complicated and the tutorial on this site would give a great starting point. I liked to use them in this case because you have to keep track of two different variables for each word (what the word is and the number of times it occurs). By using a class (or struct) we can tell the computer that the variables are related and we can access them in relation to eachother.
- I said don't check for eof() because my method of getting the words was to use the extraction operator. So if you had a whitespace at the end of your file then the operator would never take that out of the buffer and technically the read would never finish.
But this has it's limitations since at the end of the file it will feed the data you last read into "Temp" through the loop again so you need to blank it at the end of each iteration.
- In the case of a class object, the data members and functions are private by default, this means that no operation outside the scope of the object can reference these pieces. By writing "public:" I am telling the system that I want operations outside of the scope of the class to be able to refer to these members.
Alright, I understand the class part now due to looking at the tutorial and your help. The only thing I am stuck on now is blanking the Temp after each iteration. It would go in the for loop correct? I am also confused as to what "blanking" it means. Is it closing it of assigning it a zero value?
Also when while(Line>> Temp) is put it, it says that Line is undefined, however if I put in an ifstream, it doesnt pick it up.
Ooops, my bad. I didn't mean for you to delete your declarations. Most of the stuff you had should have stayed. I wrote "//More Of Your Code" because I didn't want to type it out again.
EDIT: So you still need the line of code that reads: ifstream Line("KeyWordsOnLineHelp.txt");
Also, the last thing I need is to cout the words that were repeated and how many times. Would that statement go before the while or before the for statement?
That would go AFTER the while(...) statement. This code needs to read through the entire document before it will know how many times each word is repeated. You'd use a loop to iterate through the vector and output the data.
Not sure what you mean, so I would use a while loop to have it output the words and the number of times they appear? Also, is Temp what will be put into the vector to iterate through?
An "std::vector<class T>" container is a dynamic array of Templates, don't worry if that sounds scary it's simpler then the description implies. See here for a complete reference: http://www.cplusplus.com/reference/stl/vector/
As with any array you can access any of its elements by it's index number. I suggest using a for loop for this as it allows you to first initialize an integer variable to zero, then compare that variable against a condition (in this case while the integer is less then the size of the vector), and finally promote the integer by one. Inside the loop you would use the integer to access the elements of the array sequentially that is starting at the index number 0, then going to the end of the array.
The member function size() is already part of the std::vector container and is updated pretty much everytime the size of the container changes (there are specific exeptions to this that do not apply to this example).