Cannot figure out how to do a word count

Hello,

Here's the details:

User inputs sentence
Program checks sentence for 3 magic words ("chicken", "egg", "rooster")
Program keeps count of every magic word found and displays results

Here's my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include<iostream>
#include<string>
using namespace std;

const string chicken( "chicken" );
const string egg( "egg" );
const string rooster( "rooster" );
const string quit( "FINISHED" );

int main () 
{

	string inputSentence;
	int w1Found, w2Found, w3Found;

	cout << "Type a sentence" << endl;
	cin >> inputSentence;

	do {

		if ( inputSentence == chicken )
		{

			w1Found++;
			

		} else if ( inputSentence == egg )
		{

			w2Found++;

		} else if ( inputSentence == rooster )
		{

			w3Found++;

		}

		cin >> inputSentence;

	} while ( inputSentence != quit );

	cout << "\"" << chicken << "\" strings found: " << word1found << endl;
	cout << "\"" << egg << "\" strings found: " << word2found << endl;
	cout << "\"" << rooster << "\" strings found: " << word3found << endl;

}
Problem with this code is that it requires EVERY sentence typed to end with "FINISHED" for it to work and know when to stop counting. I need to figure out a better way. Like:

hello blah blah chicken chicken blah egg

"chicken" found: 2
"egg" found: 1
"rooster" found: 0

But now I have to do hello blah blah chicken chicken blah egg FINISHED for it to work
I think your going to have to have some kind of escape sequence like FINISHED. Just make a a semi-colon or something that is only one keystroke.
Basically it should begin the word count as soon as the enter key is pressed, which it doesn't do if you dont include a FINISHED at the end. And it should stop at the last word (all without including any extra libraries).

So this is what happens now..

--> hello bye whats up chicken egg egg FINISHED

(1 chicken found, 2 egg, 0 rooster)

--> hello bye FINISHED

(0 everything)

--> FINISHED

(0 everything)

--> okay read this
--> comon read it and do the word count already
--> omg what the chicken is going on
--> still not reading
--> okay let me type in FINISHED

(1 chicken found)

Last edited on
Use a map<string, int>. It's the simplest way to do a word-count.
Sentences are normally terminated by a full stop (.), an exclamation point (!), or a question mark (?), and they may or may not be bracketed with parentheses () or square-brackets [].

Why not just check to see if the last, non-bracket character is one of the sentence terminators? If it is, then you know to stop reading.


A couple of other comments. I don't know if your professor has asked you to do this or not, but things like
1
2
  #define one 1
  const string rooster = "rooster";
etc. are quantifiably, er, stupid --though you will find such spread around a lot of code. If you are checking for a rooster, check for the rooster directly, instead of using some "constant" that someone may have changed on recompilation.
else if (inputWord == "rooster")

The other comment is about how you check the input word. What if the user inputs:
I wish I were a rooster, but I am not.
or
Roosters make for happy hens.
(sorry :-P)
In both cases, the simple == operator will fail to identify them. You need to
1 Normalize them (convert the input to upper- or lower-case), and
2 Search for them using one of the string::find() functions.

Hope this helps.
Last edited on
Topic archived. No new replies allowed.