455,907 Members | 1,283 Online
Need help? Post your question and get tips & solutions from a community of 455,907 IT Pros & Developers. It's quick & easy.

# reading cin - finding duplicate words

 P: n/a 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 '05 #1
11 Replies

 P: n/a tm****@gmail.com 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 Nov 1 '05 #2

 P: n/a So here's my code. It's not very interesting at this point. #include #include using namespace std; int main() { //declaring strings string s1; //declare a vector for the words vector 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 '05 #3

 P: n/a oops i forgot the #include Nov 1 '05 #4

 P: n/a wrote in message news:11**********************@g43g2000cwa.googlegr oups.com... So here's my code. It's not very interesting at this point. #include #include using namespace std; int main() { //declaring strings string s1; //declare a vector for the words vector 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 Nov 1 '05 #5

 P: n/a tm****@gmail.com wrote: So here's my code. It's not very interesting at this point. #include #include using namespace std; int main() { //declaring strings string s1; //declare a vector for the words vector 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 ), 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 Nov 1 '05 #6

 P: n/a wrote in message news:11**********************@f14g2000cwb.googlegr oups.com... 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. Nov 1 '05 #7

 P: n/a 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 '05 #8

 P: n/a 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 '05 #9

 P: n/a On Tue, 01 Nov 2005 14:52:37 -0800, tm****@gmail.com 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 . - Jay Nov 1 '05 #10

 P: n/a tm****@gmail.com 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. Nov 2 '05 #11

 P: n/a On 2005-11-01, tm****@gmail.com wrote: So here's my code. It's not very interesting at this point. #include #include using namespace std; int main() { //declaring strings string s1; //declare a vector for the words vector 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 Nov 3 '05 #12

### This discussion thread is closed

Replies have been disabled for this discussion.