In C++, input and output (I/O) operators are used to take input and display output.
The operator used for taking the input is known as the extraction or get from operator (>>),
while the operator used for displaying the output is known as the insertion or put to operator (<<).
Input Operator
The input operator, commonly known as the extraction operator (>>), is used with the standard input stream, cin.
Program to demonstrate the working of an input operator.
#include<iostream.h>
#include<conio.h>
voiid main ()
{
int a;
cin>>a;
a = a+1;
getch();
}
In this example, the statement cin>> a takes an input from the user and stores it in the variable a.
Output Operator
The output operator, commonly known as the insertion operator (<<), is used. The standard output stream cout Like cin, cout also treats data as a stream of characters.
Program to demonstrate the working of an output operator.
#include<iostream.h>
#include<conio.h>
void main ()
{
int a;
cin>>a;
a=a+1;
cout<<a;
getch();
}
Cascading of Input/Output Operators
The cascading of the input and output operators refers to the consecutive occurrence of input or output operators in a single statement.
Program without cascading of the input/output operator.
#include<iostream.h>
#include<conio.h>
void main ()
{
int a, b;
cin>>a;
cin>>b;
cout<<"The value of a is";
cout<<a;
cout<<"The value of b is";
cout<<b;
getch();
}
In this example, all cin and cout statements use separate input and output operators respectively However, these statements can be combined by cascading the input and output operators accordingly as shown in this example.
Program with cascading of the input/output operator
#include<iostream.h>
#include<conio.h>
int main ()
{
int a, b;
cin>>a>>b;
cout<<"The value of b is : "<<b;
cout<<"The value of a is "<<a;
return 0;
}
The Un-Buffered Standard Error Stream (cerr)
The object cerr is an instance of ostream class and it is tied to the standard error device, which display error message on the screen. Object cerr is an un-buffered so each stream inserted displayed immediately on output device.
#include <iostream>
using namespace std;
int main()
{
cerr<<"Error occurred!"<<endl;
return 0;
}
Output:
Error occurred!
The Standard Log Stream (clog) – Buffered error stream
The object clog is an instance of ostream class and it is tied to the standard error device, which display error message on the screen but the object clog is buffered. This means that each insertion to clog will be held in a buffer until the buffer is filled or until the buffer is flushed.
#include <iostream>
using namespace std;
int main()
{
clog<<"Error Occurred!"<<endl;
return 0;
}
Output:
Error Occurred!
Note:If you look at above three examples of cout, cerr, clog there is no significant difference between them.
Some important manipulators in <iomanip.h> are of 2 types
1.Manipulator operator
eg: endl - end the line
ends- give a single space between two objects
cout<<"ab"<<ends<<"cd";
2.Manipulator functions
eg: setw(), setbase(),setfill(), setprecision(), setonflag(),
setw (val): It is used to set the field width in output operations.
setbase(val): It is used to set the numeric base value for numeric values.
setfill (c): It is used to fill the character ‘c’ on output stream.
setprecision (val): It sets val as the new value for the precision of floating-point values.
setiosflags(flag): It is used to set the format flags specified by parameter mask.
resetiosflags(m): It is used to reset the format flags specified by parameter mask.
Program to illustrate manipulators
#include <iostream.h>
#include<conio.h>
#include <iomanip.h>
void main()
{
clrscr();
cout<<"ab"<<ends<<"cd"<<endl;
cout << setprecision(2) << 1023.26556 << endl;
cout << setbase(8)<<62<<endl;
cout << setbase(16)<<62<<endl;
cout << setbase(10)<<62<<endl;
cout << setw(10)<<setiosflags(ios::left)<<"Hello"<<endl;
cout << setw(10)<<setiosflags(ios::right)<<"Hello"<<endl;
cout << setw(10)<<setiosflags(ios::oct)<<65;
cout << setw(10)<<setiosflags(ios::hex)<<65;
cout << setw(10)<<setiosflags(ios::dec)<<65;
cout << setfill('*')<<endl;
cout <<setw(6)<<"rama"<<endl;
getch();
}
output:
No comments:
Post a Comment