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:

#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

Recommended Answers

All 3 Replies

use getline to read the inputSentence from cin. This will stop reading when it gets the enter key.

put the entered sentence into a stringstream.

read the words out of the stringstream while there are words to read.

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

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

int main ()
{

	string inputSentence, word;
	stringstream ss;
	int w1Found=0, w2Found=0, w3Found=0;

	cout << "Type a sentence" << endl;

	getline(cin, inputSentence);
   ss << inputSentence;

	while(ss >> word){
      cout << "'" << word << "'" << endl;

		if ( word == chicken )
		{

			w1Found++;


		} else if ( word == egg )
		{

			w2Found++;

		} else if ( word == rooster )
		{

			w3Found++;

		}


	};

	cout << "\"" << chicken << "\" strings found: " << w1Found << endl;
	cout << "\"" << egg << "\" strings found: " << w2Found << endl;
	cout << "\"" << rooster << "\" strings found: " << w3Found << endl;

}

Sorry I should've said this before. This is for a beginnering C++ class, I'm not allowed to import things I dont know about. All we learned is including the cmath, iostream and string libraries so I cant use anything that would actually make my life easier...yet.

Once again sorry.

OK. The following is how I might do it:

you can still use getline(cin, inputLine); which means you won't have to type in FINISHED, just the enter key.

inputLine will now contain all the words in the sentence separated by spaces, you will then need to get each word from the line. You can use the string::find(start_pos, " ") to find the position of the spaces, and the string::substr(start_pos, length) to get the word from the line.

You could instead use string::find_first_of(start_pos, " \r\n\t") to find the position of the next whitespace (space, carriage return, line feed, tab)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.