How to use setfill() in <iomanip> more fluently?

135 views Asked by At

Today, I learned about setfill() and other methods in #include<iomanip>, so I have a practice with using #include<iomanip>.

I want to print this on the console:

+-------------------------+         
|    Iomanip Examples     |  
+-------------------------+         

So I wrote this code:

cout << '+' << setfill('-') << setw(25) << '+' << endl;
cout << setw(26) << internal << "Iomanip Examples" << endl;
cout << '+' << setfill('-') << setw(25) << '+' << endl;

but my console printed this:

+---------------------------+     //It's okay.  
------Iomanip Examples           //nope..  
+---------------------------+  

I think when I use setfill('-'), the output stream is filled with '-' character in the empty spaces.

How can I solve this problem?

1

There are 1 answers

3
pradeexsu On

you again need to use setfill(' ') for the second line as in your case. once we use setfill() it will set for all-time in program where we use setw().