Professional Documents
Culture Documents
Object Oriented Programming With C++: Click To Add Text
Object Oriented Programming With C++: Click To Add Text
Object Oriented Programming With C++: Click To Add Text
istream ostream
stream
Device
iostream
Character manipulation
char c;
cin.get(c); //it can read ‘\n’ and blankspace
cout.put(c);
cout.put(65); // prints A
String manipulation
char st[30];
cin.getline(st,6); // c++ is fun
cout<<st<<endl; // c++ i (including the terminating null character)
strcpy(st,”bangla”);
cout.write(st,4); //bang
cout.write(st,10); //bangla_ _ _ _
cout<<st<<endl; // bangla
Output formatting
C++ supports 3 ways of formatting
1. ios class functions & flags.
2. Manipulator
3. User defined output functions / manipulators.
ios class format function: width
return 0;
}
ios class format function: setf
int main() Output:
{ +_ _275.500
//display trailing zero
cout.setf(ios::showpoint);
// display + sign
cout.setf(ios::showpos);
cout.precision(3);
//fixed point notation
cout.setf(ios::fixed, ios::floatfield);
//padding after sign
cout.setf(ios::internal,ios::adjustfield);
cout.width(10);
cout<<275.5<<endl;
return 0;
}
Unsetting format flag
#include <iostream>
using namespace std;
int main()
{
cout.setf(ios::uppercase | ios::scientific);
cout << 100.12; // displays 1.001200E+02
cout.unsetf(ios::uppercase); // clear uppercase
cout << " \n" << 100.12; // displays 1.001200e+02
return 0;
}
Manipulator
Manipulators are special functions that can
be included in the I/O statements.
The header file iomanip contains
manipulator functions.
Manipulator Example
#include<iostream> Output:
#include<iomanip> _ _ _52
using namespace std;
int main()
{
int x=52;
cout<<setw(5)<<x<<endl;
return 0;
}
Manipulator Example
int main()
{
cout.fill(‘*’)
cout<<setw(5)<<setprecision(2)<<2.1678<<endl
<<setw(10) <<setprecision(4) <<2.1678<<endl
<<setw(15)<< setiosflags(ios::scientific)<<2.1678<<endl;
return 0;
} Output:
**2.2
*****2.168
****2.1678e+000
Manipulator / ios member
There is an important difference between
manipulator and ios member function.
The ios member function return the previous
format state which can be saved and stored
later on. This cannot be done with
manipulators.
Example:
cout.precision(2);
int p = cout.precision(4);
cout.precision(p);
Writing user defined manipulator
#include<iostream> Output:
#include<iomanip>
using namespace std; ###52