reading cin - finding duplicate words

Discussion in 'C++' started by tmshaw@gmail.com, Nov 1, 2005.

  1. Guest

    I'm a newb in a c++ class...

    I need to read the standard input into a couple strings. Then determine
    whether or not the same word is used IN SUCCESSION (ex. this cat cat is
    really mean.).

    Then count and print the word and how many times it was used. So using
    the above example, it would print out the words that were used in
    succession and how many times they were used. For example, the word cat
    was used 2 times inthe above example.

    I'm pretty much stuck on what sort of test to run to compare two or
    more unknown words for sameness....

    we aren't very far in the class past strings and vectors...

    thanks for any help.
    , Nov 1, 2005
    #1
    1. Advertising

  2. mlimber Guest

    wrote:
    > I'm a newb in a c++ class...
    >
    > I need to read the standard input into a couple strings. Then determine
    > whether or not the same word is used IN SUCCESSION (ex. this cat cat is
    > really mean.).
    >
    > Then count and print the word and how many times it was used. So using
    > the above example, it would print out the words that were used in
    > succession and how many times they were used. For example, the word cat
    > was used 2 times inthe above example.
    >
    > I'm pretty much stuck on what sort of test to run to compare two or
    > more unknown words for sameness....
    >
    > we aren't very far in the class past strings and vectors...
    >
    > thanks for any help.


    Try using std::string. If it doesn't work, show us what you have so
    far, and we can give you some pointers. We're not going to do your
    homework for you, however.

    Cheers! --M
    mlimber, Nov 1, 2005
    #2
    1. Advertising

  3. Guest

    So here's my code. It's not very interesting at this point.

    #include <iostream>
    #include <string>
    using namespace std;

    int main()
    {
    //declaring strings
    string s1;

    //declare a vector for the words
    vector<string> v1;

    //aquiring input from stdinput, into string, into vector
    cout << "Please enter a sentence and press ENTER: " << endl;

    while (cin >> s1)
    v.push_back(s1);

    So, do I want to do comparisons to find duplicates at the while/cin
    stage?

    if so, i'm not sure how to keep track of them... can I tack on
    statements between the "while cin" and the "v.pushback" part?

    grrr. I dont even know the right questions to ask.
    , Nov 1, 2005
    #3
  4. Guest

    oops i forgot the #include <vector>
    , Nov 1, 2005
    #4
  5. <> wrote in message
    news:...
    > So here's my code. It's not very interesting at this point.
    >
    > #include <iostream>
    > #include <string>
    > using namespace std;
    >
    > int main()
    > {
    > //declaring strings
    > string s1;
    >
    > //declare a vector for the words
    > vector<string> v1;
    >
    > //aquiring input from stdinput, into string, into vector
    > cout << "Please enter a sentence and press ENTER: " << endl;
    >
    > while (cin >> s1)
    > v.push_back(s1);
    >
    > So, do I want to do comparisons to find duplicates at the while/cin
    > stage?


    Look up unique_copy. There must be usage examples out there. Josuttis's The
    C++ Standard Library has a good example too.

    Ali
    =?iso-8859-1?Q?Ali_=C7ehreli?=, Nov 1, 2005
    #5
  6. mlimber Guest

    wrote:
    > So here's my code. It's not very interesting at this point.
    >
    > #include <iostream>
    > #include <string>
    > using namespace std;
    >
    > int main()
    > {
    > //declaring strings
    > string s1;
    >
    > //declare a vector for the words
    > vector<string> v1;
    >
    > //aquiring input from stdinput, into string, into vector
    > cout << "Please enter a sentence and press ENTER: " << endl;
    >
    > while (cin >> s1)
    > v.push_back(s1);
    >
    > So, do I want to do comparisons to find duplicates at the while/cin
    > stage?


    I'd suggest instead reading a whole line and then parsing it:

    if( getline( cin, s1 ) )
    {
    // Now s1 contains the entire input, up until the user hit enter.
    // Do parsing of s1 here.
    }

    With the code you supplied, the program would just keep reading strings
    until the state of cin goes bad, which only happens when it reaches end
    of file (which wouldn't happen unless the user dumped a file to your
    program, e.g., from the command line "myprog < file.txt") or when there
    is an error on the device (which probably won't happen in your
    environment). In short, it would never end (though at some point you'd
    run out of memory).

    As for the parsing of s1, you might use an istringstream (include
    <sstream>), but I'm not sure if your professor wants you to do that.
    Something like:

    istringstream ss( s1 );
    string s2;
    while( ss >> s2 )
    {
    v.push_back(s2);
    // Do comparisons, increment counters, and so forth
    }

    >
    > if so, i'm not sure how to keep track of them... can I tack on
    > statements between the "while cin" and the "v.pushback" part?


    Yes. You just need to use braces as in my code above.

    >
    > grrr. I dont even know the right questions to ask.


    There are probably easier ways to do it than with vector and string,
    but I expect that your professor wants you to use them for this
    assignment.

    Cheers! --M
    mlimber, Nov 1, 2005
    #6
  7. Jim Langston Guest

    <> wrote in message
    news:...
    > I'm a newb in a c++ class...
    >
    > I need to read the standard input into a couple strings. Then determine
    > whether or not the same word is used IN SUCCESSION (ex. this cat cat is
    > really mean.).
    >
    > Then count and print the word and how many times it was used. So using
    > the above example, it would print out the words that were used in
    > succession and how many times they were used. For example, the word cat
    > was used 2 times inthe above example.
    >
    > I'm pretty much stuck on what sort of test to run to compare two or
    > more unknown words for sameness....
    >
    > we aren't very far in the class past strings and vectors...
    >
    > thanks for any help.
    >


    Consider this. Put each word in a vector, so you have a vector of
    std::string, each string containing a word. Then iterate through the vector
    checking if this current word is equal to the one before it if it is
    increment a counter.

    Pseudo code:
    std::string lastword = "";
    int concurrant = 0;
    for (iterate through vector)
    if ( thiscurrentword == lastword )
    concurrant += 1;
    else
    {
    if ( concurrant != 0 )
    std:cout << "word " << lastword << " used " << concurrant << "
    times." << std::endl;
    concurrant = 0;
    }
    }

    that's pretty much the logic I would use.
    Jim Langston, Nov 1, 2005
    #7
  8. Guest

    Jim: that's sort of the logic I was expecting; ie, it looks like the
    sort of thing we have been doing in class.

    Thanks for the help everyone. It's always amazing/confusing to me to
    see how many different ways things can be done in programming. I guess
    I always thought things were way more "rigid" when it comes to writing
    this stuff.
    , Nov 1, 2005
    #8
  9. Guest

    Mlimber: This is one of the things that annoys me about our text: it
    never tells you the whole story. It tells you to do things one way,
    then 3 chapters later, you find it out that it is wrong to do it that
    way.

    so... on the subject of infinite loop. I have been trying to look up
    what sort of command you use to "break out of" waiting for user input.

    when I am using the linux system at school and I run my compiled
    programs, the teacher told us to just hit ctrl-d after entering input
    from the keyboard. After I do that, the next part of the code runs.

    I can't figure out how to make the same thing happen when I run my
    progs from the command prompt on my windows system.
    , Nov 1, 2005
    #9
  10. Jay Nabonne Guest

    On Tue, 01 Nov 2005 14:52:37 -0800, wrote:

    > Mlimber: This is one of the things that annoys me about our text: it
    > never tells you the whole story. It tells you to do things one way,
    > then 3 chapters later, you find it out that it is wrong to do it that
    > way.
    >
    > so... on the subject of infinite loop. I have been trying to look up
    > what sort of command you use to "break out of" waiting for user input.
    >
    > when I am using the linux system at school and I run my compiled
    > programs, the teacher told us to just hit ctrl-d after entering input
    > from the keyboard. After I do that, the next part of the code runs.
    >
    > I can't figure out how to make the same thing happen when I run my
    > progs from the command prompt on my windows system.


    On Windows, it's <Ctrl-Z><Enter>.

    - Jay
    Jay Nabonne, Nov 1, 2005
    #10
  11. Old Wolf Guest

    wrote:
    > Jim: that's sort of the logic I was expecting; ie, it looks like the
    > sort of thing we have been doing in class.


    Note - when posting in comp.lang.c++ , please quote relevant pieces
    of what you're replying to. Other people use different reader
    software that might not show the older messages in the thread.

    > Thanks for the help everyone. It's always amazing/confusing to me to
    > see how many different ways things can be done in programming. I guess
    > I always thought things were way more "rigid" when it comes to writing
    > this stuff.


    >From what you've said, there is no need to use a vector. The

    only thing your code has to remember is the most recent string
    read. Then all you have to do is read a new string, compare it
    to the previous one (using == ), increment a counter if it is
    equal, and then store the new string as the most-recent-string.
    Wash, rinse, repeat.
    Old Wolf, Nov 2, 2005
    #11
  12. Neil Cerutti Guest

    On 2005-11-01, <> wrote:
    > So here's my code. It's not very interesting at this point.
    >
    > #include <iostream>
    > #include <string>
    > using namespace std;
    >
    > int main()
    > {
    > //declaring strings
    > string s1;
    >
    > //declare a vector for the words
    > vector<string> v1;
    >
    > //aquiring input from stdinput, into string, into vector
    > cout << "Please enter a sentence and press ENTER: " << endl;
    >
    > while (cin >> s1)


    You may need to define what a word is better than that. A string
    doesn't necessarily correspond to a word. The standard input
    stream is going to say, for example, that "cat" and "cat," (note
    the comma) are different words.

    If you'll only accept lists of words with no punctuation allowed,
    i.e., won't handle real prose, then you should document that.

    In addition, will "cat" and "Cat" be considered the same word?

    I suggest documenting that you accept just a plain list of words,
    and that you internally translate every character to lower case.
    That is, unless the assignment rules that out.

    > v.push_back(s1);
    >
    > So, do I want to do comparisons to find duplicates at the
    > while/cin stage?
    >
    > if so, i'm not sure how to keep track of them... can I tack on
    > statements between the "while cin" and the "v.pushback" part?
    >
    > grrr. I dont even know the right questions to ask.


    You should get your professor to tell you what the output should
    be for text like:

    cat cat dog cat cat

    Is it

    1) "cat": 2, "cat": 2

    or

    2) "cat": 4

    If 1 is the answer, then you don't need a vector at all.

    If 2 is the answer, then you can use a vector, but using a map
    would be less complex.

    --
    Neil Cerutti
    Neil Cerutti, Nov 3, 2005
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. TaiwanNoWhere

    cin and cin.clear() problem

    TaiwanNoWhere, Oct 13, 2003, in forum: C++
    Replies:
    8
    Views:
    4,091
    P.J. Plauger
    Oct 17, 2003
  2. Chris Mantoulidis

    std::cin.ignore() and std::cin.clear()

    Chris Mantoulidis, Jan 6, 2004, in forum: C++
    Replies:
    5
    Views:
    17,137
    Kevin Saff
    Jan 6, 2004
  3. Aleander

    cin and cin.getline()

    Aleander, Mar 6, 2005, in forum: C++
    Replies:
    5
    Views:
    8,666
    Alex Vinokur
    Mar 6, 2005
  4. Fernando
    Replies:
    4
    Views:
    1,650
    Fernando
    Nov 16, 2011
  5. Voetleuce en fĂȘnsievry

    How to remove duplicate words from text

    Voetleuce en fĂȘnsievry, Jul 22, 2003, in forum: Javascript
    Replies:
    7
    Views:
    446
    Richard Cornford
    Aug 2, 2003
Loading...

Share This Page