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.
#include void main() { int i; //Declaration statement cin>>i; //Input statement cout<<i; //Output statement }
Example:
Illustrates the use of cin and cout
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:
cin>> val1>>val2;
The values are assigned from left to right.
Example
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
//cin.cpp #include void main() { char name[10]; char category; int telno; cout<<"Enter name, category and telephone no. "; cout<<'\n'; cout<<"Type y if category is student otherwise type n \n "; cin>>name>>category>>telno; cout<<"Name Category Tel number "; cout<<'\n'; cout<<name<<" "<<category<<"" <<telno; cout<<'\n'; }
‘\n’ is used for passing control to the next line.