by Dinesh Thakur Category: Object-oriented programming (OOP)

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.

Example

 

Illustrates the use of cin and cout

 

#include <iostream.h>

void main()

{

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:

                                                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 <iostream.h>

 

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.

 



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles