#include #include #include #include "CPstring.h" // file: formatwatch.cc; author: D. Ramm; date: 10/15/96 // file input; while with EOF to exit // some word processing // move stats to function with reference parameters void Watch(string word, int & wc, string w1, int & c1, string w2, int & c2) // precondition: another word is presented in parameter word, w1 and w2 // are words to watch for. // postcondition: wc contains total word count word and c1 and c2 are the // number of times that w1 and w2 (resepctively) have been encountered. { wc++; if (word == w1) { c1++; } if (word == w2) { c2++; } } int main() { string margin, word, watch1, watch2; int k, cnt1 = 0, cnt2 = 0, wcnt = 0; string filename, maring = ""; ifstream win; const int LFTMARGIN = 10; const int COLWIDTH = 40; int leng, position = 0; cout << "Enter file name: "; cin >> filename; win.open(filename); for (k = 0; k < LFTMARGIN; k++) margin += " "; cout << endl << endl << margin; watch1 = "I"; watch2 = "do"; while (win >> word) { Watch(word, wcnt, watch1, cnt1, watch2, cnt2); leng = word.length(); position += leng + 1; if (position > COLWIDTH) { position = leng + 1; cout << endl << margin; } cout << word << " "; } cout << endl << endl; cout << "For " << wcnt << " words read from file: " << filename << endl; cout << "Count for \"" << watch1 << "\" was " << cnt1 << endl; cout << "Count for \"" << watch2 << "\" was " << cnt2 << endl; return 0; } Sample output: formatwatch Enter file name: words I wish upon a star, that someone else would do all of the grading that I do, and do it from afar. I wish I could, I wish I could... For 30 words read from fie: words Count for "I" was 6 Count for "do" was 2 cat words I wish upon a star, that someone else would do all of the grading that I do, and do it from afar. I wish I could, I wish I could...