Questions: 1. Explain manipulators. 2. List ios functions with equivalent manipulators in C++. Explain any one manipulator with example.
Manipulators
•
Manipulators are the operators in C++ that are used for formatting the output.
Various commonly used manipulators are setw,
endl and setfill.
•
The setw is used to set the minimum field width. We can set the right justified
numbers using setw operator. Note that the
setw function is defined in <iomanip.h>
file.
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int a=4444,b=22,c=333;
cout<<"Using setw(30)\n";
cout<<setw(30)<<a<<"\n";
cout<< setw(30)<<b<<"\n";
cout<< setw(30)<<c<<"\n";
cout<<"Using setw(20)\n";
cout<<setw(20)<<a<<"\n";
cout<<setw(20)<<b<<"\n";
cout<<setw(20)<<c<<"\n";
cout<<"Using setw(10)\n";
cout<<setw(10)<<a<<"\n";
cout<<setw(10)<<b<<"\n";
cout<< setw(10)<<c<<"\n";
retrun 0;
}
Output
Using setw(30)
4444
22
333
Using setw(20)
4444
333
22
Using setw(10)
4444
22
333
•
The functionality of endl is similar
to "\n" i.e. newline character.
C++ Program
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
cout<<"This is first line"<<endl;
cout<<"This is second line"<<endl;
cout<<"This is third line"<<endl;
cout<<"This is forth line"<<endl;
return 0;
}
Output
This is first line
This is second line
This is third line
This is forth line
• The setfill manipulator is used to fill the
fields entirely using some character specified within it.
C++ Program
#include<iostream>
#include<iomanip>
using namspace std;
int main()
{
int a=4444,b=22,c=333;
cout<<setw(10)<<setfill('*')<<a<<endl;
cout<<setw(10)<<setfill('*')<<b<<endl;
cout<<setw(10)<<setfill('*')<<c<<endl;
retrun 0;
}
Output
******4444
********22
*******333
Example:1
i) Write output for the following (Assume all
libraries has been included):
1. int
main()
{
float f = ‒76.54321;
cout.fill ('*');
cout.precision(3);
cout.setf(ios::internal, ios::adjustfield);
cout.setf(ios::scientific, ios:floatfield);
cout.width(15);
cout << f<< "\n";
}
2. int
main()
{
float f = 123.4;
cout.setf(ios::showpoint);
cout.setf(ios::showpos);
cout.precision(3);
cout.setf(ios::fixed, ios::floatfield);
cout.setf(ios::internal, ios:adjustfield);
cout.width(10);
cout << f << "\n";
}
Solution :
We
will include two library files namely iostream.h and iomanip.h
1.
‒ ****7.654e+001
2.
+ 123.400
Example:2
Write a
program which reads the information like name, age and weight of ten people
from the keyboard and displays it on the screen in four columns: Sr. No, Name,
Weight and Age. Strings should be left‒justified and numbers should be right‒justified
in a suitable field width.
Solution :
#include <iostream>
#include<iomanip>
using namespace std;
#define MAX 10
class student
{
private:
char Name[30];
int age;
float weight;
public:
void getDetails(void);
void
putDetails(int);
};
//member function definition, outside of the class
void student::getDetails(void)
{
cout <<
"Enter name:
cin >> Name;
cout <<
"Enter age: ";
cin >> age;
cout <<
"Enter Weight:
cin >> weight;
}
//member function definition, outside of the class
void student::putDetails(int sr_No)
{
cout
<<"\n"<<< setw(5)<<left<<sr_No
<<<setw(8)<<left<<
Name
<<< setw(8)
<<right<< age
<< setw(8)
<<right<< weight;
}
int main()
{
student std[MAX];
//array of objects creation
int n,i;
cout << "Enter total number of students:
cin >> n;
for(i=0;i<n;i++)
{
cout <<
"Enter details of person " << i+1 << ":\n";
std[i].getDetails();
}
cout << endl;
cout<<"\n Sr.\tName\tAge\tWeight";
for(i=0;i< n; i++)
{
std[i].putDetails(i+1);
}
return 0;
}
Example:3
What is
basic difference between manipulators and ios member functions in
implementation? Give examples.
Solution:
1)The
ios function returns a value while manipulators does not.
2)
There are standard ios functions we can have them user defined, while our own
standard ios functions we can have them user manipulators can be created.
3)
The ios function needs <iostream> while manipulators need <iomanip>
4)
The ios functions are single and cannot be combined, while it is possible to
specify the manipulators in chain.
Examples
of ios are ‒
i)
cout ii) cin
iii) cerr iv) clog
Examples
of manipulators ‒
i)
endl ii) setw
iii) setfill iv) ws
1. Explain
manipulators.
2. List ios functions
with equivalent manipulators in C++. Explain any one manipulator with example.
Object Oriented Programming: Chapter 6: IO Systems and File IO : Tag: Oops, Computer Programming : C++ Object Oriented Programming - Manipulators
Object Oriented Programming (OOPs)
CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation
English Essentials II
EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Tamils and Technology தமிழர்களும் தொழில்நுட்பமும்
UC25H02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Linear Algebra
MA25C02 2nd Semester | 2025 Regulation
Applied Physics (CSIE) II
PH25C03 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Digital Principles and Computer Organization
CS25C06 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Basic Electrical and Electronics Engineering
EE25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Object Oriented Programming (OOPs)
CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation
Re-Engineering for Innovation
ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Object Oriented Programming (OOPs) - Laboratory
CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation