Formatting output is important in development of output screens, which can be easily read and understood. C++ offers several i/o manipulators. Two of them are
The function setw() which is used to set the width by specifying width as the parameter. In other words, it sets the width of the field assigned for the output. It takes the size of the field (in number of characters) as a parameter.
Setw can be used in many ways. Its main goal is to provide the width to as the result in our output. If we want to format our output results then it can be used as a pretty good function. Let’s just assume that we have a program, we want to print the name of the user as the winner or anything. So, it will be great if we print the name in a good format like we can print the name at the centre of the console.
setw() accepts integer arguments as parameters and the integer can be any integer. The integer argument sets the width to be set. Setw does not return anything as output. It can only act as a stream manipulator. For example
cout << setw(6) << “R”;
Note : In order to use this manipulator, you must include header file iomanip.h
It does not stick from one cout statement to the next. You will need to repeat setw() for each value.