////////////////////////////////////////////////////////////////// // // getword.cpp // // Started by Jeff Ondich on 4/1/98 // Last modified 4/1/98 // // This program retrieves words from standard input // until reaching "end of file." You can run this // in two ways: // // (1) getword < somefile.txt // // (2) getword // // In this case, you will type the input. You // can type as many lines as you like. To terminate // the input, type CTRL-D at the start of a new line. // ////////////////////////////////////////////////////////////////// #include #include bool GetWord( string& word ); int main( void ) { string myWord; while( GetWord( myWord ) ) { cout << myWord << endl; } return( 0 ); } ////////////////////////////////////////////////////////////////// // // GetWord retrieves the next word from standard input (cin), // returning true if there is a word remaining in the input, // and false if not. In the former case, the word is // returned via the reference parameter "word". // // GetWord defines a "word" to be any contiguous block of // letters, hyphens, and apostrophes. Thus, "don't" and // "willy-nilly" are both considered words (as they // should be), but so is "--'--xxxx-'". Defining "word" // better than this is possible, but complicated. // // We'll talk about how GetWork works some time in class. // ////////////////////////////////////////////////////////////////// bool GetWord( string& word ) { char c; cin.get( c ); while( !cin.fail() && !isalpha(c) && c != '-' && c != '\'' ) cin >> c; if( cin.fail() ) return( false ); word = c; cin.get( c ); while( !cin.fail() && (isalpha(c) || c == '-' || c == '\'') ) { word = word + c; cin.get( c ); } return( true ); }