In: Computer Science
C++ give some examples of where you could use the stream manipulators to format output.
//examples.cpp
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
//endl
//this flushes the output stream and also acts as new
line
cout<<"- - - - - - - - endl - - - - - - -
-\n\n";
cout<<"Following Line appers in new
Line:"<<endl<<"Hi Iam printing in new
line"<<endl;
cout<<"\n\n- - - - - - - - - - - - - - -
-\n\n";
//setw(n)
//setw() manipulator changes the width of the next
input/output field.
//it sets the width parameter of the stream out or in
to exactly n.
//it prints the n number of white spaces
cout<<"\n\n- - - - - - setw(n) - - - - - - - -
-\n\n";
cout <<"without setw(n): "<< 120 <<
" " << 12<<endl;
cout <<"With setw(15) : "<< 120 <<
setw(15) << 12<<endl;
cout<<"\n\n- - - - - - - - - - - - - - -
-\n\n";
//setprecision(n)
//sets the floating point precision to be
displayed
cout<<"\n\n- - - - - - setprecision(n) - - - - -
- - - - -\n\n";double pi = 3.141592653589793239;
cout << "default precision (6) of PI : "
<< pi << '\n';
cout << "std::setprecision(10) of PI: " <<
setprecision(10) << pi << '\n';
cout<<"\n\n- - - - - - - - - - - - - - -
-\n\n";
//showpoint/noshowpoint
//used to show a decimal point or not
cout<<"\n\n- - - - - - showpoint/noshowpoint - -
- - - - - - - -\n\n";
cout<<"No ShowPoint of 11.0 :
"<<noshowpoint<<11.0<<endl;
cout<<"ShowPoint of 11.0 :
"<<showpoint<<11.0<<endl;
cout<<"\n\n- - - - - - - - - - - - - - -
-\n\n";
return 0;
}