Example 3 shows you how to write a manipulator that takes two arguments. As you can see, it is similar to the previous example except that the character pointer type declaration is replaced by a structure declaration.
Example 3
The following program illustrates the definition of the fill manipulator, which inserts a specified quantity of a particular character:
// exios203.cpp
// 2-argument manipulator example
#include <iostream.h>
#include <iomanip.h>
struct fillpair {
char ch;
int cch;
};
IOMANIPdeclare( fillpair );
ostream& fp( ostream& os, fillpair pair )
{
for ( int c = 0; c < pair.cch; c++ ) {
os << pair.ch;
}
return os;
}
OMANIP(fillpair) fill( char ch, int cch )
{
fillpair pair;
pair.cch = cch;
pair.ch = ch;
return OMANIP (fillpair)( fp, pair );
}
void main()
{
cout << "10 dots coming" << fill( '.', 10 ) << "done" << endl;
}
Example 3 could be easily rewritten with the manipulator definition in a separate program file. The header file must contain the neccesary declarations as follows:
struct fillpair {
char ch;
int cch;
};
IOMANIPdeclare( fillpair );
ostream& fp( ostream& o, fillpair pair );
OMANIP(fillpair) fill( char ch, int cch );