Using the fill() member function and setfill() manipulator C++ program sample

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: Using the fill() member function and setfill() manipulator for padding, left and right justified and internal justification in C++ programming

To show: How to use the fill() member function and setfill() manipulator for padding, left and right justified and internal justification in C++ programming

 

// using fill() member function and setfill() manipulator

#include <iostream>

#include <iomanip>

using namespace std;

 

void main(void)

{

long p = 30000;

cout<<p

<<" printed using the default pad character\n"

<<"for right and left justified and as hex\n"

<<"with internal justification.\n"

<<"--------------------------------------------\n";

 

cout.setf(ios::showbase);

cout<<setw(10)<<p<<endl;

cout.setf(ios::left,ios::adjustfield);

cout<<setw(10)<<p<<endl;

cout.setf(ios::internal,ios::adjustfield);

cout<<setw(10)<<hex<<p<<"\n\n";

cout<<"Using various padding character"<<endl;

cout<<"-------------------------------"<<endl;

cout.setf(ios::right,ios::adjustfield);

cout.fill('#');

cout<<setw(10)<<dec<<p<<'\n';

cout.setf(ios::left,ios::adjustfield);

cout<<setw(10)<<setfill('$')<<p<<'\n';

cout.setf(ios::internal,ios::adjustfield);

cout<<setw(10)<<setfill('*')<<hex<<p<<endl;

}

 

Output example:

 

30000 printed using the default pad character

for right and left justified and as hex

with internal justification.

--------------------------------------------

30000

30000

0x 7530

Using various padding character

-------------------------------

#####30000

30000$$$$$

0x****7530

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index