/* Program Name: Word Counter Process: asks how many sentences the user would like to input, counts the number of words within a sentence, using classes/objects prints out word count before exiting program. Function Input/Parameters: ??? Function Output/Parameters: ??? Function Output/Returned: ??? Device Input/Device: ??? Device Output/Monitor: ??? Dependencies: ??? */ #include #include #include #include using namespace std; // function prototypes // NONE // class definitions // NONE // main program int main(int argc, char **argv) { int numSentences = 0; int numSpaces = 0; string tempString; // ask user for number of sentences cout << "How many sentences would you like to enter?" << '\n'; cin >> numSentences; cout << endl; // for loop for each sentence, count the words by counting the number of spaces, print results for (int sentenceIterator = 0; sentenceIterator < numSentences; sentenceIterator++) { // ask user for sentence text cout << "Enter text for sentence " << sentenceIterator + 1 << ": " << endl; // ignore whitespace, put ALL whitespace into cin. cin.ignore(); // get the input from user, dump into tempString getline(cin, tempString); // count number of spaces for(int characterIterator = 0; characterIterator < tempString.size(); characterIterator++) { // iterate through string, count if there is space. if(tempString[characterIterator] == ' ') { // count space numSpaces = numSpaces + 1; } } // add an extra 'word' because of the end. numSpaces = numSpaces + 1; // print out to the user cout << "Sentence " << sentenceIterator + 1 << " has " << numSpaces << " words." << endl; // reset word counter numSpaces = 0; } // return 0 return 0; } // compiles using "g++ -Wall -pedantic wordcounter.cpp -o wordcounter" - note to programmer