Linear search that adds word

I'm writing a program that allows the user to enter data into an external file, that data when read into the file is then sorted to list in alphabetical order how many words are in it and tallying words that are used more than once. My problem is coming from when I read in a word to the file I want it to be tallied in a prototype function but everywhere I go to learn how to do this it only teaches you how to return whether the word is in the array or not. This is what i've come up with but I know it's wrong:

1
2
3
4
5
6
7
8
9
10
void searchWord (tempWord)
{
  for (int i = 0; i < list; i++)
  {
    if (list [i] == tempWord)
      temptally++;
    else
      list [i] = (list [i] + tempWord)
  }
} 


Your thoughts and input will be much appreciated!

Are you trying to count how many times tempWord value comes in the list? Is list a single dimensional array?
The user will enter a word, the function will then search the array (single dim) to see if the word is already there, if it is, the function will increase the number of occurences of that word by 1, if not, it will add it to the end of the list.
I suggest using a vector.
http://www.cplusplus.com/reference/stl/vector/vector/

Just push the words into a vector then search for the word in that vector. If it exists then increase the count of that word. Otherwise push that word into the vector.

I also suggest creating a word struct which contains the string value of the word and the count. Use that when inserting into your vector.
 
struct word{int count; string value;};

http://www.cplusplus.com/forum/beginner/4606/
Topic archived. No new replies allowed.