Page 35 - Modular_V1.1_Flipbook
P. 35
04 INPUT AND OUTPUT
IN C++
Your Aim
to learn about:
Output Input
Escape Sequence Characters Comments
Every program needs some input to work and displays some output after processing the input.
C++ also allows you to give input to a program and get the output from the program. Every
input/output in C++ is considered as a stream of characters. C++ provides two objects to take
the input and to display the output which are cin and cout. Let us discuss these in detail.
OUTPUT
The cout object is used to display the output on the output device connected to the computer
like monitor. It is an instance of the ostream class and also called Standard Output Stream. It is
used in conjunction with the stream insertion operator << (two less than signs). For example:
cout<<”Output on the screen”;
The preceding statement shows the message Output on the screen on the monitor. The stream
insertion operator can be used multiple times in a single statement to combine multiple output.
For example:
cout<<”Hello”<<” from “<<” C++”;
You can also print the variable value using the cout object. For example:
cout<<x;
cout<<120;
Chaining insertions is especially useful to mix literals and variables in a single statement:
cout<<”I am”<< name<<”. I am”<<age<<” years old.”;
In the preceding statement, the values of two variables name and age are printed with text like:
I am Chirag. I am 18 years old.
By default, cout object prints the output without any line breaks. To insert a line break, a new-line
character \n is used. It is also called escape sequence character. For example:
cout<<”I am Kittu.\n”;
cout<<”I am 14 years old.\nI study in class 10.”;
Input and Output in C++ 33

