Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to allow setw apply to all the following stdout?

Tags:

c++

Should be a trivial question, but found that setw only apply to its immediate following output, and not sure how to allow it apply to all the following output.

For example, for the following line of code

cout<<setw(3)<<setfill('0')<<8<<" "<<9<<endl;

or

cout.width(3);
cout.fill('0');
cout<<8<<" "<<9<<endl;

I want the output to be 008 009 instead of 008 9

like image 769
Hailiang Zhang Avatar asked Jun 30 '13 20:06

Hailiang Zhang


1 Answers

setw isn't sticky, so you have to say it every time:

cout << setfill('0') << setw(3) << 8 << " "
     << setw(3) << 9 << endl;
like image 114
Kerrek SB Avatar answered Dec 14 '22 21:12

Kerrek SB