Thread: repeated word detection program - help needed

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    384

    repeated word detection program - help needed

    In the C++ book Programming: Principles and Practice Using C++, there's an example code like this for detecting repeated words:

    Code:
    #include "std_lib_facilities.h"
    
    int main()
    {
        int number_of_words = 0;
        string previous = " ";       // previous word; initialized to “not a word”
        string current;                             // current word
        while (cin >> current)        // read a stream of words
        {
            if (previous == current)    // check if the word is the same as last
            {
                ++number_of_words;
                cout << "word number " << number_of_words << " repeated: " << current << "\n";
                previous = current;
            }
        }
        keep_window_open();
    }
    The header file in there is sort of like a set of training-wheels for students who are complete beginners to the language; the function keep_window_open() is defined in there and it does just that on Windows systems where the output window closes too fast (in the case of the function, it's just like cin.ignore(), except it waits for you enter a character, like 'j', before it exits); programs on my Windows laptop work fine on Code::Blocks, but when I create a .exe file for them and double-click that file, it does actually close too quickly for me to be able to see the output (if it's a program like the generic "Hello World!" program that just outputs text to the screen and then exits - so all I see is the output window just flash-by really fast in those cases).

    Anyway, as for the problem I'm having with the code: there are no error and compile- or link-time, but it does behave strangely at runtime, where the part inside the curly-braces of the while-loop doesn't execute at all. So I'm wondering where the problem is.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Do you mean the if() statement inside the while loop doesn't execute as you expect?

    I don't expect that code to ever execute, since previous will probably never equal current.

    Also your comments are mostly incorrect.

    Code:
        string previous = " ";       // previous word; initialized to a single space.
        string current;                             // current word
        while (cin >> current)        // read a word from the console.
    The previous = current; line should probably be outside the if statement.

    Jim

  3. #3
    Registered User
    Join Date
    Mar 2015
    Posts
    184
    you mean if you type "cat dog mouse" in the console nothing happens? Because [previous] is never assigned outside the if statement. So every time it checks if (" " == current) it evaluates false. Current is never equal to " " because cin is delimited by spaces.

  4. #4
    Registered User
    Join Date
    Mar 2015
    Posts
    384
    Quote Originally Posted by jimblumberg View Post
    Do you mean the if() statement inside the while loop doesn't execute as you expect?

    I don't expect that code to ever execute, since previous will probably never equal current.

    Also your comments are mostly incorrect.

    Code:
        string previous = " ";       // previous word; initialized to a single space.
        string current;                             // current word
        while (cin >> current)        // read a word from the console.
    The previous = current; line should probably be outside the if statement.

    Jim
    It's taken from Stroustrup's book exactly as is, so it probably works for him somehow but not for us. If that's even possible.

    So you're saying I should declare do previous = current outside the if-statment?

    Edit: It's working perfectly now

    I probably just got confused due to Stroustrup's lack of use of curly-braces in his if-statement and while-loop code.

    This is what he's got in there that through me off:

    Code:
    int main()
     {
              int number_of_words = 0;
              string previous = " ";                 // not a word
              string current;
              while (cin>>current) {
                        ++number_of_words;       // increase word count
                        if (previous == current)
                                  cout << "word number " << number_of_words
                                            << " repeated: " << current << '\n';
                        previous = current;
              }
     }
    You can tell that previous = current is outside the if-statment, but someone like me who is used to seeing curly-braces will probably have a hard time at first. Ah, well.

    Edit2: And by the way, that example code is an improvement on this one:

    Code:
    int main()
     {
               string previous = " ";       // previous word; initialized to “not a word”
               string current;                             // current word
               while (cin>>current) {                // read a stream of words
                         if (previous == current)    // check if the word is the same as last
                                   cout << "repeated word: " << current << '\n';
               previous = current;
               }
     }
    And that's where most of the comments came from.
    Last edited by Osman Zakir; 04-02-2015 at 02:37 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. count repeated word
    By Gaily Guzon in forum C Programming
    Replies: 4
    Last Post: 09-28-2013, 11:48 AM
  2. Replies: 2
    Last Post: 09-23-2013, 08:10 PM
  3. Replies: 28
    Last Post: 10-23-2011, 07:17 PM
  4. how to delete the repeated word
    By icable in forum C Programming
    Replies: 1
    Last Post: 05-11-2003, 06:56 PM
  5. tile collision detection help needed...
    By werdy666 in forum Game Programming
    Replies: 0
    Last Post: 01-01-2003, 02:57 AM