// Some simple manipulators. Note std C++ (1999) defines // "left" and "right" and many others already. Borland C++ v5.02 // doesn't have all the standard manipulators, but other compilers might, // so I used "Left" not "left", and "Right" not "right" here. // // Written by Wayne Pollock, Tampa, FL, 1999. #ifndef MYMANIPS_H #define MYMANIPS_H #include //-----------------------Left--------------------------------- inline ostream& Left ( ostream& os ) { os.setf( ios::left, ios::adjustfield ); return os; } //-----------------------Right-------------------------------- inline ostream& Right ( ostream& os ) { os.setf( ios::right, ios::adjustfield ); return os; } //-----------------------Flush-------------------------------- inline istream& Flush ( istream& in ) { int num_of_chars_left = in.rdbuf()->in_avail(); return in.ignore(num_of_chars_left); } //-----------------------Rewind-------------------------------- inline istream& Rewind ( istream& in ) { in.clear(); in.seekg(0); return in; } #endif
// Test program for MyManips.h // Written by Wayne Pollock, Tampa FL USA, 1999 #include #include #include #include "MyManips.h" int main () { char buf[256], word[256]; cout << "Enter a short sentence: "; cin >> buf; cout << "First word was \"" << setw(12) << Left << buf << "\". (Skipping rest of line...)\n"; cin >> Flush; cout << "Try it again, enter a few words: "; cin >> buf >> Flush; cout << "The first word on the new line was \"" << setw( 12 ) << Right << buf << "\".\n\n"; cout << "Type in one more line of text: "; cin.getline( buf, sizeof buf ); istrstream in( buf ); cout << "Here is your sentence:\n"; while ( in >> word ) cout << word << " "; cout << endl; in >> Rewind; cout << "\n\nHere is your sentence again:\n"; while ( in >> word ) cout << word << " "; cerr << "\n\nHit Enter to quit: "; cin.getline( buf, sizeof( buf ) ); return 0; }