A really simple (for you people) question about "setw" statement.

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?

Thanks.
Last edited on
See this:

http://www.cplusplus.com/reference/iomanip/setw/?kw=setw

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. That's why I have posted it here. Thanks anyway.
> 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++'.


So write a small program; something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <iomanip>

int main()
{
    std::cout << std::showpos ;

    int v = 123 ; // default fill character is space
    std::cout << std::right << '|' << std::setw(6) << v << "|\n" ;
    std::cout << std::left << '|' << std::setw(6) << v << "|\n" ;
    std::cout << std::internal << '|' << std::setw(6) << v << "|\n" ;

    v = 12345678 ; // std::setw(6) ignored
    std::cout << std::right << '|' << std::setw(6) << v << "|\n" ;
    std::cout << std::left << '|' << std::setw(6) << v << "|\n" ;
    std::cout << std::internal << '|' << std::setw(6) << v << "|\n" ;

    v = 1 ;
    std::cout << std::right << '|' << std::setfill('.') << std::setw(6) << v << "|\n" ;
    std::cout << std::left << '|' << std::setfill('*') << std::setw(6) << v << "|\n" ;
    std::cout << std::internal << '|' << std::setfill('0') << std::setw(6) << v << "|\n" ;

    std::cout << std::noshowpos ;

    std::cout << std::right << '|' << std::setfill('.') << std::setw(6) << v << "|\n" ;
    std::cout << std::left << '|' << std::setfill('*') << std::setw(6) << v << "|\n" ;
    std::cout << std::internal << '|' << std::setfill('0') << std::setw(6) << v << "|\n" ;
}

http://coliru.stacked-crooked.com/a/012dc4159eeff4db

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.
"field" means "number width+spaces".

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.

Thanks both of you.
Good luck!
Topic archived. No new replies allowed.