Link for a better explantion of setw

The explanation on Cplusplus is not in depth enough for me. Thanks in advance if anyone knows of one. ( the Microsoft one had me lost in the sauce.)
closed account (SECMoG1T)
setw is just a short form for set width, it one of c++ formatted io manipulators defined in <iomanip> , you can think of it as a flag that tells ostream objects{ostringstream,ofstream,ostream} to grant a certain field a particular amount of space when writing it to an output device, might be the console, file etc. , for example

1
2
3
4
5
6
7
8
9
10
string name("my name");
cout<<setw(10)<<name<<endl;///tells cout to ensure that when displaying the field
                                                   /// name a minimum of 10 spaces is used, in my example
                                                   ///name contains 7 characters and since it's less than ten
                                                   ///the three extra spaces will be filled by fill characters 
                                                   ///which are usually spaces
///the displayed text might look like this, "note i have used  dash(-) to denote spaces"
"---my name"
///if the field name had more than 10 characters then cout might not care about the width
///and will have to use extra spaces to display it. 


also you should not there are other manipulators like ios::right,ios::left that determines
the direction from which the fill characters will be set.

This links might give you a better idea.

http://www.dreamincode.net/forums/topic/247193-setw5-what-does-it-do/
http://www.c4learn.com/cplusplus/cpp-setw-setting-field-width/
https://www.daniweb.com/software-development/cpp/threads/114864/setw-and-setfill
I read all that and understand that much, but when my professor sent us his review sheet he included this.

1
2
3
4
double x=48.967

cout << setprecision(2)<<fixed<<setw(3)<<x<<endl;
Answer: 48.97


I am lost on the way he got that with setw(3)
> Link for a better explantion of setw

https://stdcxx.apache.org/doc/stdlibug/28-3.html

Perhaps this is what you are looking for:
-----------------------------------------------------------------------------------------------------------
NOTE -- With the exception of the field width, all format parameter settings are permanent.
The field width parameter is reset after each use.
-----------------------------------------------------------------------------------------------------------
closed account (SECMoG1T)
Yeah that's the right answer because of the precision he provided
setprecision(2) , in your example setw(3) doesn't count because cout is forced to use some extra space to display 48.97
So when u use cout w setprecision it ignores setw? ( i didn't find the answer in the link above)
No. It does not.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <iomanip>

int main()
{
    const double v = 12.34567 ;

    std::cout << std::fixed << std::setprecision(3)
    
              << std::setfill('0') << std::setw(10) << v << '\n' // 000012.346
              
              << v << '\n' // 12.346 // default width; setw(10) is no longer in efffect
              
              << std::setw(12) << v << '\n' // 00000012.346 // setfill('0') is still in effect
              
              << std::setw(5) << v << '\n' ; // 12.346 // width of 5 is too small; setw(5) is ignored
}

http://coliru.stacked-crooked.com/a/8248fd368dd4598d
closed account (SECMoG1T)
Hey, i dint say that using setprecision causes setw() to be ignored, each of those three manipulators in your example have a specific role in formatting the output

cout << setprecision(2)<<fixed<<setw(3)<<x<<endl;

1. setprecision(2)
this tells cout to use a precision of 2 when displaying the floating point field that follows
that is two decimal places after the '.', if your number have more than two digits after the
decimal point, then the number is rounded off to two decimal point, if it have less than two
or non, then zeros are used as padding

2. fixed
this tells cout to display floating point values in decimal notation instead of scientific
notation

3. setw(3)
this tells cout to use a minimum of 3 spaces to display the field that follows
i.e 48.97 but because this value is larger that three characters cout will have to use
some extra spaces when displaying it and your width 3 is ignored

4. endl
this adds a newline character to the cout stream and then flushes it's buffer.



check the examples given by @JLBorges, it got some perfect explanation
Last edited on
After reading andy's and JLBorges I have a better understanding on this.
Topic archived. No new replies allowed.