Create custom output manipulator and a custom input manipulator called skip_digits(). - C++ File Stream

C++ examples for File Stream:cout

Description

Create custom output manipulator and a custom input manipulator called skip_digits().

Demo Code

#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>
using namespace std;
// A simple output manipulator that sets the fill character
// to * and sets the field width to 10.
ostream &star_fill(ostream &stream) {
   stream << setfill('*') << setw(10);
   return stream;
}
// A simple input manipulator that skips leading digits.
istream &skip_digits(istream &stream) {
   char ch;/*w  w  w .  j  ava 2s.c  o  m*/
   do {
      ch = stream.get();
   } while(!stream.eof() && isdigit(ch));
   if(!stream.eof()) stream.unget();
   return stream;
}
int main()
{
   string str;
   // Demonstrate the custom output manipulator.
   cout << 512 << endl;
   cout << star_fill << 512 << endl;
   // Demonstrate the custom input manipulator.
   cout << "Enter some characters: ";
   cin >> skip_digits >> str;
   cout << "Contents of str: " << str;
   return 0;
}

Result


Related Tutorials