reading cin - finding duplicate words

T

tmshaw

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.
 
M

mlimber

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
 
T

tmshaw

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.
 
?

=?iso-8859-1?Q?Ali_=C7ehreli?=

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
 
M

mlimber

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
 
J

Jim Langston

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.
 
T

tmshaw

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.
 
T

tmshaw

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.
 
J

Jay Nabonne

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
 
O

Old Wolf

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.
 
N

Neil Cerutti

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top