cin and cout are two predefined objects which represent standard input and output stream. The standard output stream represents the screen, while the standard input stream represents the keyboard. These objects are members of iostream class. Hence the header file <iostream.h> should be included in the beginning of all programs.
Illustrates the use of cin and cout
int i; //Declaration statement
cin>>i; //Input statement
cout<<i; //Output statement
The operator << is called the insertion operator and the operator >> is called the extraction operator.
Cascading of I/O Operators
It is possible to use the insertion and extraction operators repeatedly in cin and cout statements. Multiple use of these operators is called Cascading. For example, the extraction operator can be cascaded as follows:
cout<< b1<<" "<<b2;
This statement sends firsts the value of b1 to cout, two blank spaces and then the value of b2 to cout. The insertion operator can be cascaded as follows:
The values are assigned from left to right.
The program takes input for name, category and telephone number of a person using a single cin statement. Input for the category is given as y if the person is a student, otherwise input is given as n. The program also illustrates Cascading of operators
cout<<"Enter name, category and telephone no. ";
cout<<"Type y if category is student otherwise type n \n ";
cout<<"Name Category Tel number ";
cout<<name<<" "<<category<<"" <<telno;
'\n' is used for passing control to the next line.