Another useful formatting function is "width", which tell the stream the number of spaces to use to output the next item. For example, if the integers num1, num2 and num3 have values 1, 12 and 123, respectively, then the code
cout << "Number1";
cout.width(5);
cout << num1 << endl;
cout << "Number2";
cout.width(5);
cout << num2 << endl;
cout << "Number3";
cout.width(5);
cout << num3 << endl;
will produce the following output on the screen:
Number1 1
Number2 12
Number3 123
The width function affect only the next item that is output. This is why we needed to call width three times in the above example.
The entire item is always output, no matter what argument is given to width. If the output requires more space than you specified in the argument to width, then as much additional space as is needed will be used.