How many spaces does "setw" statement leave? (more like "How this statement works?")
I coded like setw(4) and it left only 1 space. In a new program I coded the same and it left 2 spaces. I don't know how this works. Can someone explain it to me please?
it determines the field width. That means it adds spaces (or other character: see setfill) only when the original output chracters are less than the field width.
> Well I already saw the page before but couldn't understand it properly.
The most effective way to increase your knowledge is to try new problems in a controlled way. Pick one aspect of C++ that you haven't understood before and write a program that, aside from using that one aspect, uses only things that you have already mastered. Then do what it takes to understand what your program is doing - and why. - Andrew Koenig and Barabara Moo in 'Ruminations on C++'.
Modify it in various ways; play around with different combinations of std::setw(), std::setfill() and different types (int, double, string). Till you are convinced that you have understood what std::setw() does.
If your number was 100, then setw(4) would add 1 space.
If your number was 10, setw(4) would add 2 spaces.
If your number was 1, setw(4) would add 3 spaces.
@ ShodanHo Oh, so that's how it is. Thanks. I'll keep it in my mind now.
@ JLBorges
The most effective way to increase your knowledge is to try new problems in a controlled way. Pick one aspect of C++ that you haven't understood before and write a program that, aside from using that one aspect, uses only things that you have already mastered. Then do what it takes to understand what your program is doing - and why. - Andrew Koenig and Barabara Moo in 'Ruminations on C++'.
I always experiment like that. But the thing that was puzzling me was the "field", and ShodanHo explained it really good and simple.