Cpp - Output Field Width

Introduction

The field width is the number of characters that can be written to a field.

If the output string is larger than the field width, the output is not truncated and the field is extended.

The output will contain at least the number of digits specified as the field width.

You can either use the width() method or the setw() manipulator to define field width.

cout.width(6);    // or:  cout << setw(6); 

The field width setting is non-permanent: the field width specified applies to the next output only.

The default field width is 0. You can use the width() method to get the current field width.

int fieldwidth = cout.width();