Trinity PU College, Mysuru Computer Science
Chapter-9
INPUT AND OUTPUT OPERATORS
Input Operator “>>”:
The standard input device is usually the keyboard. Input in C++ is done by using the “stream
extraction” (>>) on the cin stream. The operator must be followed by the variable that will
store the data that is going to be extracted from the stream.
Example:
int age;
cin>>age;
The first statement declares a variable of the type int called age, and the second one waits for
an input from cin (the keyboard) in order to store it in this integer variable. cin stands for
“console input”. It can only process the input from the keyboard once the RETURN key has
been pressed.
Output Operator “<<”: The standard output device is the screen (Monitor). Outputting in
C++ is done by using the object followed by the “stream insertion” (<<). cout stands for
console output.
Example: cout<<” Let us learn C++”; // prints Let us learn C++ on the screen.
The << operator inserts the data that follows it into the stream preceding it. The sentence in
the instruction is enclosed between double quotes (”), because it is constant string of
characters. Whenever we want to use constant strings of characters, we must enclose them
between double quotes (“) so that they can be clearly distinguished from the variables name.
Cascading of I/O Operators:
C++ supports the use of stream extraction (>>) and stream insertion (<<) operator many
times in a single input (cin) and output (cout) statements. If a program requires more than one
input variable then it is possible to input these variables in a single cin statement using
multiple stream extraction operators.
Trinity PU College, Mysuru Computer Science
Formatted Output (Manipulators):
Manipulators are the operators used with the insertion operator <<to format the data display.
The most commonly used manipulators are endl and setw.
1. The endl manipulator: The endl manipulator, when used in an output statement,
causes a line feed to be inserted. It has same effect as using new line character “\n”.
2. The setw() Manipulator: The setw() manipulator sets the width of the field assign for
the output. It takes the size of the field (in number of character) as a parameter. The
output will be right justified.
Example the code: cout<<setw(6)<<”R” ;
Generates the following output on the screen (each underscore represents a blank space)
_____R
In order to use this manipulator, it is must to include header file iomanip.h
Simple Program:
Program 1: To fins the sum of two numbers:
#include<iostream.h>
#include<conio.h>
void main ()
{
int a, b, add;
clrscr();
cout<<” Enter the two numbers”;
cin>>a>>b;
add = a + b; cout<<” The sum of two number is” <<sum<<endl;
getch();
}