Write a program to count how many times each distinct word appears in its input .

This is a discussion on Write a program to count how many times each distinct word appears in its input . within the C++ Programming forums, part of the General Programming Boards category; Write a program to count how many times each distinct word appears in its input . Some trouble here as ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    62

    Write a program to count how many times each distinct word appears in its input .

    Write a program to count how many times each distinct word appears in its input .

    Some trouble here as well. This is what I am trying:
    Code:
    #include<iostream>
    #include<string>
    #include<vector>
    
    using std::cout;        using std::cin;
    using std::string;      using std::vector;
    using std::endl;
    
    int main()
    {
        cout << "Please enter the word to look for: ";
    
        vector<string>text;
        string x;
    
        while(cin >> x)
        text.push_back(x);
    
        cout << "Now enter the word to look for: ";
        string word;
        cin >> word;
    
        return 0;
    }
    Now ater escaping rom putting in a new word in the text by pressin ctrl+z it just jumps over word not giving me the ability to give in a new word.

  2. #2
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    892
    Maybe you need a cin.ignore() in there? I'd have some in-program way of escaping from the input, anyway -- like "Enter \"!done\" when finished."
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    Entering control-Z to terminate the while loop effectively puts your program's cin stream into an error state. Any attempts to use the cin stream after that point are ignored. To resolve this, you need to clear the stream's state via the clear function. This can be done at any point after the while loop and before your next attempt to use the cin stream.

    Code:
    while(cin >> x)
        text.push_back(x);
    
    cin.clear();
    
    cout << "Now enter the word to look for: ";
    string word;
    cin >> word;
    [edit]FYI, this type of problem is perfect for a map<string,int> container.[/edit]
    Last edited by hk_mp5kpdw; 07-28-2010 at 02:10 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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a troublesome C++ program
    By Kristina in forum C++ Programming
    Replies: 4
    Last Post: 05-18-2002, 01:28 PM
  2. Word Count gone kookoo :)
    By Spectrum48k in forum C Programming
    Replies: 2
    Last Post: 05-17-2002, 09:12 PM
  3. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM
  4. word count program need a bit of help!
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 04-19-2002, 08:15 PM
  5. word count
    By gokila in forum C Programming
    Replies: 2
    Last Post: 02-19-2002, 12:35 PM

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