Output formatting in C++

Formatting Cout output in C++ using iomanip.h header file:
  • iomainp of standard form is “Input and Output Manipulation” header file.
  • It is used to align the sentence or output on output screen.
  • Standard methods are,setw(),setfill() and setprecision().
1. setw():
  • This function is used to set the space on the output.
  • Setw means set width.
Syntax:
             cout<<”Some sentence”<<setw(size);
2.setfill():
  • This is function is used to display number of symbols with given width.
Syntax:
             cout<<”Some sentence”<<setfill(any symbol)<<setw(size);
3.setprecision()
  • This fuction is used to take decimal value based on its size.
Syntax:
             cout<<”Some sentence”<< setprecision(size) << number or variable;
Example program:
  1. #include<iostream.h>
    #include<conio.h>
    #include<iomanip.h>
    void main()
    {
    clrscr();
    cout<<endl;//next line
    cout<<" Display 12345 : "<<setw(5)<<1<<setw(5)<<2<<setw(5)<<3<<setw(5)<<4<<setw(5)<<5<<endl<<endl;//setw()
    cout<<setfill('*')<<setw(50)<<" Welcome to whereisstuff.com "<<setfill('*')<<setw(50);//setfill()
    cout<<"\n\n 15.78963 of two decimal value is: "<<setprecision(2)<<15.78963<<endl;// setprecision()
    getch();
    }
Output:

Note:
  • endl is used to enter purpose or goto next line in the output screen(It mainly used for, when double-quotes do not use in cout function and that output of result needs to go to the next line means we give endl).
  • \n – it is also used to go next line if give \n then must be within "".


Share:

No comments:

Post a Comment

Popular Posts

Search This Blog

Powered by Blogger.

Blog Archive

Service Support

Need our help to Learn or Post New Concepts Contact me

Blog Archive

Recent Posts

Service Support

Need our help to Learn or Post New Concepts Contact me