C++ program to use various member functions of ios classes to format output

//width, precision,fill,setf,unsetf

#include <iostream>
using namespace std;
int main()
{
    char name[]="Deep Raj Bhujel";
    int n=123;
    float f=12.345;

   
//width    cout<<endl<<"Use of width"<<endl;
    cout.width(20);
    cout<<name<<endl;
    cout.width(10);
    cout<<n<<endl;
    cout.width(15);
    cout<<f<<endl;

    //precision
    cout<<endl<<"Use of precision"<<endl;
    cout.precision(3);
    cout<<f<<endl;

    //fill
    cout<<endl<<"Use of fill"<<endl;
    cout.fill('*');
    cout.width(20);
    cout<<name<<endl;
    cout.width(10);
    cout<<n<<endl;
    cout.width(15);
    cout<<f<<endl;

    //setf
    cout<<endl<<"Use of setf"<<endl;
    cout.fill('*');
    cout.setf(ios::left,ios::adjustfield);
    cout.width(20);
    cout<<name<<endl;
    cout.setf(ios::right,ios::adjustfield);
    cout.width(20);
    cout<<name<<endl;
    cout<<"The number in hexadecimal is: ";
    cout.setf(ios::hex,ios::basefield);
    cout<<n<<endl;

    //unsetf
    cout<<endl<<"Use of unsetf"<<endl;
    cout.fill('*');
    cout.setf(ios::left,ios::adjustfield);
    cout.unsetf(ios::left);
    cout.width(20);
    cout<<name<<endl;

    return 0;
}

No comments:

Post a Comment