Cpp - Output Fill Characters and Alignment


If a field is larger than the string you need to output, blanks are used by default to fill the field.

You can either use the fill() method or the setfill() manipulator to specify another fill character.

cout << setfill('*') << setw(5) << 12; 
// Output: ***12 

The fill character applies until another character is defined.

Output to fields is normally right-aligned.

The other options available are left-aligned and internal, which can be set by using the manipulators left and internal.

The manipulator internal left-justifies the sign and right-justifies the number within a field.

cout.width(6); cout.fill('0'); 
cout << internal << -123; // Output: -00123