To set the width of the output field.
streamsize width() const streamsize width(streamsize wide)
Use the width() function to set the field size for output. The function is overloaded to return just the current width setting if there is no parameter or to store and then return the previous setting before changing the fields width to the new parameter.
Width is the one and only modifier that is not sticky and needs to be reset with each use. Width is reset to width(0) after each use.
The previous width setting is returned.
#include <iostream> int main() { using namespace std; int width; cout.width(8); width = cout.width(); cout.fill('*'); cout << "Hi!" << '\n'; // reset to left justified blank filler cout<< "Hi!" << '\n'; cout.width(width); cout<< "Hi!" << endl; return 0; }
Result:
Hi!***** Hi! Hi!*****