Setfill in c++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Setfill in c++

What does setfill in c++ do? Should we always use it with setw?

7th Nov 2019, 2:00 PM
Armina
Armina - avatar
7 Answers
+ 4
Armina It is a manipulator that is used to specify the character to be used as padding when the value/object to be printed isn't wide enough to fill up the width specified by `std::setw` manipulator. Run this code to see the difference, where padding is added to the left or right. #include <iostream> #include <iomanip> int main() { std::cout << "fill left padding\n"; std::cout << std::setfill('*') << std::setw(7) << 2019; std::cout << "\nfill right padding\n"; std::cout << std::left << std::setfill('*') << std::setw(7) << 2019; return 0; }
7th Nov 2019, 3:13 PM
Ipang
+ 3
Armina `std::setfill` is meant to be used in tandem with `std::setw`. One defines the padding character, and the other defines the desired width (in characters) for the output. Seems to me, `std::setfill` doesn't effect without `std::setw`. I didn't understand your idea with the '\t' tab character, can you show me in a code please?
7th Nov 2019, 3:34 PM
Ipang
+ 2
Armina The tab character '\t' does appear as if it pushes the next data deeper this way. But then we can see that the specified padding character '*' is not being used to pad the output. So I guess they don't get along that well : )
7th Nov 2019, 3:59 PM
Ipang
+ 2
No problem Armina I'm glad if it helps 👍
7th Nov 2019, 4:03 PM
Ipang
+ 1
Ipang Thanks , is setfill used always after setw or it can be used after any spaces like"\t"?
7th Nov 2019, 3:19 PM
Armina
Armina - avatar
+ 1
#include <iostream > #include <iomanip > using namespace std; int main() { cout<<"\t"<<setfill('*')<<2019<< endl; return 0; } I mean instead of using setw for spaces, use " \t "
7th Nov 2019, 3:48 PM
Armina
Armina - avatar
+ 1
Thanks Ipang, you helped me alot
7th Nov 2019, 4:01 PM
Armina
Armina - avatar