C++ IOS and Manipulator
C++ IOS and Manipulator
2
IOstream Library Header files
We use the #include <iostream.h>
Contains basic infor required for all stream-I/O
operations
cin, cout, cerr, clog
#include <iomanip.h>
formatted I/O with parameterized stream manipulators
#include <fstream.h>
file processing operations
3
I/O File Class Hierarchy
Base Class Multiple inheritance
Input
ios
Output
istream ostream
iostream
iostream.h
fstream.h
ifstream fstream ofstream
File I/O
4
Unformatted I/O
Performed with read and write member functions
They take two parameters
an array of char or a buffer
a number which tells how many characters
cin.get(c);
cout.put(c);
cin.read(buffer,20);
cin.write (buffer,15);
5
getline Member Functions
cin.getline(array, size)
1 // Fig. 21.14: fig21_14.cpp
2 // Character input with member function
3 #include <iostream>
getline.
4
5 using std::cout;
6 using std::cin;
7 using std::endl;
8
9 int main()
10{
11 const SIZE = 80;
12 char buffer[ SIZE ];
13
14 cout << "Enter a sentence:\n";
15 cin.getline( buffer, SIZE );
16
17 cout << "\nThe sentence entered is:\n" <<
18 return
buffer 0;
<< endl;
19}
Enter a sentence:
Using the getline member function
The sentence entered is:
Using iostream.h
Include iostream.h instead of stdio.h
Standard iostream objects:
cout - object providing a connection to the monitor
cin - object providing a connection to the keyboard
cerr - object providing a connection to error streem
To perform input and output we send messages to
one of these objects (or one that is connected to a
file)
The Insertion Operator (<<)
To send output to the screen we use the insertion
operator on the object cout
Format: cout << Expression;
The compiler figures out the type of the object and
prints it out appropriately
cout << 5; // Outputs 5
cout << 4.1; // Outputs 4.1
cout << “String”; // Outputs String
cout << ‘\n’; // Outputs a newline
Chaining Calls
Multiple uses of the insertion and extraction
operator can be chained together:
cout << E1 << E2 << E3 << … ;
cin >> V1 >> V2 >> V3 >> …;
Equivalent to performing the set of insertion or
extraction operators one at a time
Example
cout << “Total sales are $” << sales << ‘\n’;
cin >> Sales1 >> Sales2 >> Sales3;
FORMATTED CONSOLE I/O OPERATIONS
C++ supports a number of features that
could be used for formatting the output. These
features include:
ios class functions and flags.
Manipulators.
User-defined output functions.
Function Task
setw() width()
setprecision() precision()
setfill() fill()
setiosflags() setf()
resetiosflags() unsetf()
Defining Field Width: width ()
5 4 3 1 2
Setting Precision:precision()
By default ,the floating numbers are printed
with six digits after the decimal
point.However ,we can specify the number of
digits to be displayed after the decimal point
while printing the floating-point numbers.
This can be done by using the precision()
member function as follows:
cout.precision(d);
Where d is the number of digits to the
right of the decimal point.
Default precision is 6 digits.
For example ,the statements
cout.precision(3);
cout<<sqrt(2)<<”\n”;
cout<<3.14159<<”\n”;
cout<<2.50032<<”\n”;
will produce the following output:
1.141(truncated)
3.142(rounded to the nearest cent)
2.5(no trailing zeros)
Filling and Padding :fill()
** * * * * 5 2 5 0
1 2 3 4 5 * * * * * * * * * *
The statements
cout.fill(‘*’);
cout.precision(3);
cout.setf(ios::internal,ios::adjustfield);
cout.setf(ios::scientific,ios::floatfield);
cout.width(15);
cout<<-12.34567<<”\n”;
will produce the following output:
- * * * * * 1 . 2 3 5 e + 0 1
Displaying Trailing Zeros And Plus Sign
If we print the numbers 10.75, 25.00 and
15.50 using a field width of, say, eight
positions, with two digits precision, and then
the output will be as follows:
1 0 . 7 5
2 5
1 5 . 5
Certain situations, such as a list of prices of
items or the salary statement of employees,
require trailing zeros to be shown. The above
output would look better if they are printed as
follows:
10.75
25.00
15.50
+ 2 7 5 . 5 0 0
MANAGING OUTPUT WITH MANIPULATORS
The header file iomanip provides a set of functions
called manipulators which can be used to
manipulate the output formats.
cout<<setw(10)<<12345;
cout<<setw(10)<<setiosflags(ios::left)<<12345;
One statement can be used to format output for two or more
values.
For example,the statement
cout<<setw(5)<<setprecision(2)<<1.2345
<<setw(10)<<setprecision(4)<<sqrt(2)
<<setw(15)<<setiosflags(ios::scientific)
<<sqrt(3)<<endl;
Will print all the three values in one line with the field
size of 5,10,and 15 respectively.
There is a major difference in the way the
manipulators are implemented as compared to the
ios member functions. The ios member function
return the previous format state which can be used
later. In case, we need to save the old format states,
we must use the ios member function rather than
the manipulators.
Example:
cout.precision(2);//previous state
int p=cout.precision(4);//current state;
When these statements are executed, p will
hold the value of 2(previous state) and the new
format state will be 4.We can restore the
previous format state as follows:
cout.precision(p)//p=2
Designing Our Own Manipulators