Keeping track of words inputted

This is a discussion on Keeping track of words inputted within the C++ Programming forums, part of the General Programming Boards category; Like the topic says I am trying to keep track of how many times a word is typed. I am ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    2

    Keeping track of words inputted

    Like the topic says I am trying to keep track of how many times a word is typed. I am using 2 vectors one to keep the unique words and another to hold the number of times each word has been typed in.
    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    #include <cstdlib>
    using std::cout;
    using std::cin;
    using std::string;
    using std::endl; 
    using std::vector;
     
    " int main(){
          vector<string> word;
         vector<int> number;
         bool copy;int y,z;
         string input;
         vector<string>::size_type svec;
    
    cout<< "enter a word"<< endl; 
    cin>>input;
    number.push_back(1);
    word.push_back(input);
     cout<< "enter a word"<< endl; 
     while(cin>>input){int svec =0;
                       copy =false;
                       for(svec =0;svec<word.size();svec++)
                            {if(input==word[svec])
                                {copy=true; number[svec] +=number[svec]+1;}        
                            }
                      if(!copy)                           {
                         word.push_back(input);
                         number.push_back(1);
                                                              }
                                }
        
        y=number.size();         
         for(z=0;z<y;z++)
        cout<<word[z]<< " " <<number[z] << endl;             
        system("PAUSE");
        return EXIT_SUCCESS;                 
                      }
    The above is what I have coded, when I input something like:
    "bob
    bob
    bob
    bob"
    and then end input I get this: bob 15. I know there is something obvious that I'm not getting. Any help would be appreciated.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Code:
    number[svec] +=number[svec]+1;
    That's the problem, use = instead of +=.

    With the += this is what you are doing:
    Start with 1.
    1 + 2 = 3
    3 + 4 = 7
    7 + 8 = 15
    Get it?

    Easier to use a map<string,int> container:

    Code:
    #include <iostream>
    #include <string>
    #include <map>
    #include <cstdlib>
    using std::cout;
    using std::cin;
    using std::string;
    using std::endl; 
    using std::map;
     
    int main()
    {
        string input;
        map<string,int> words;
        map<string,int>::const_iterator cit;
        
        cout << "enter a word" << endl; 
        while( cin >> input )
            words[input]++;
        
        for( cit = words.begin(); cit != words.end(); ++cit )
            cout<< cit->first << " " << cit->second << endl;
    
        system("PAUSE");
        return EXIT_SUCCESS;                 
    }
    The words will be nicely sorted alphabetically for you as well using such a map.
    Last edited by hk_mp5kpdw; 01-11-2006 at 09:00 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    2
    Yes I see what you are saying. I must have gotten confused as I typed. Thanks for the help, I'll definatley look into maps and their usage.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginners Contest #2 For those who wanted more!!
    By ILoveVectors in forum Contests Board
    Replies: 16
    Last Post: 08-12-2005, 01:03 AM
  2. Problem with malloc() and sorting words from text file
    By goron350 in forum C Programming
    Replies: 11
    Last Post: 11-30-2004, 10:01 AM
  3. New Theme
    By XSquared in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 04-01-2004, 08:00 PM
  4. Keeping track of static external structure
    By pwilfred in forum C Programming
    Replies: 6
    Last Post: 03-13-2003, 06:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21