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.
-----------------------------------------------------------------------------------------------------------
NOTE -- With the exception of the field width, all format parameter settings are permanent.
The field width parameter is reset after each use.
-----------------------------------------------------------------------------------------------------------
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
#include <iostream>
#include <iomanip>
int main()
{
constdouble 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
}
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