TUT4 Iomanip

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

PRACTICAL TUTORIAL: FORMATTING OUTPUT

DO THIS
Go to textbook pg 156, do

PROGRAMMING EXAMPLE:
Movie Tickets Sale and Donation to Charity

PAY ATTENTION TO THE OUTPUT FORMATTING STATEMENTS USING


IOMANIP HEADER
SETPRECISION
SETW
SETFILL
LEFT
RIGHT

<iomanip> header contains several functions to format the output.


The main ones among them include:

 Setprecision: This function sets the precision for decimal or float values.
 setw: Setw function sets the field width or number of characters that are to be displayed before a
particular field.
 Setfill: Setfill function is used to fill the stream with char type c specified as a parameter.
Given below is a detailed C++ example to demonstrate the setprecision function.

#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
double float_value =3.14159;
cout << setprecision(4) << float_value << '\n';
cout << setprecision(9) << float_value << '\n';
cout << fixed;
cout << setprecision(5) << float_value << '\n';
cout << setprecision(10) << float_value << '\n';
return 0;
}

OUTPUT;
The setw function is demonstrated using a C++ program.

#include <iostream>
#include <iomanip>
using namespace std;
int main () {
cout << "The number printed with width 10"<<endl;
cout << setw(10);
cout << 77 << endl;

cout << "The number printed with width 2"<<endl;


cout << setw(2);
cout << 10 << endl;

cout << "The number printed with width 5"<<endl;


cout << setw(5);
cout << 25 << endl;
return 0;
}
OUTPUT

Given below is an example C++ program to demonstrate setfill.

#include <iostream>
#include <iomanip>
using namespace std;
int main () {
cout << setfill ('*') << setw (10);
cout << 15 << endl;
cout << setfill ('#') << setw (5);
cout << 5 << endl;
cout << setfill ('#') << setw (5);
cout << 1 << endl;
cout << setfill ('*') << setw (10);
cout << 25 << endl;
return 0;
}

OUTPUT

You might also like