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

Dani AI

Generated

Short diagnosis: 's loop is reading single tokens with operator>>, so the program treats each input as a whole word and expects FINISHED as a token to stop. Counters (w1Found, etc.) are never initialized (they must start at 0). The final cout uses names like word1found that don't exist. correctly suggested using getline to read a full sentence; since <sstream> is not allowed in this class, tokenization can be done with a simple character scan and light normalization instead of stringstream or extra headers.

A compact, beginner-friendly approach that uses only <iostream> and <string>: read the entire line with getline, walk the characters building a word buffer, convert A–Z to a–z manually, drop punctuation by only keeping alphanumerics, and check each completed word against the three magic words. Counters are initialized to 0 and incremented when matches occur.

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

int main() {
    const string chicken("chicken"), egg("egg"), rooster("rooster");
    string line;
    int c1 = 0, c2 = 0, c3 = 0;

    getline(cin, line); // reads whole sentence
    string w;
    for (size_t i = 0; i <= line.size(); ++i) {
        char ch = (i < line.size()) ? line[i] : ' ';
        if (ch >= 'A' && ch <= 'Z') ch = ch - 'A' + 'a';
        if ((ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9')) {
            w.push_back(ch);
        } else if (!w.empty()) {
            if (w == chicken) ++c1;
            else if (w == egg) ++c2;
            else if (w == rooster) ++c3;
            w.clear();
        }
    }

    cout << "\"chicken\" found: " << c1 << endl;
    cout << "\"egg\" found: " << c2 << endl;
    cout << "\"rooster\" found: " << c3 << endl;
}

Notes: this handles "chicken," and "Chicken" correctly. For multiple input lines, repeat the parse per getline (e.g., loop until an empty line or EOF). If the set of keywords grows later, switch to a map or vector for easier management once those headers/containers are taught.

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.