Coupon Accepted Successfully!


Input and Output Streams

The input stream uses cin object to read data and the output stream uses cout object to display the data on the screen. The cin and cout are pre-defined streams for input and output data. The data type is identified by these functions using operator overloading of the operators << (insertion operator) and >> (extraction operator). The operator << is overloaded in ostream class and the operator >> is overloaded in istream class. Figure shows the flow of input and output stream.

Description: 128119.png

Working of cin and cout statements

Input Stream

The input stream reads operation through keyboard. It uses cin as object. The cin statement uses >> (extraction operator) before a variable name. The cin statement is used to read data through the input device. Its syntax and example are as follows.




int v1;

float v2;

char v3;

cin>> v1>> v2>> v3…>> vn;


where v1, v2, and v3 are variable names. The response of the user to this statement would be as shown below.


2 5.4 A // input data


If the user enters data in the manner 2 5.4 A, then the operator will assign 2 to v1, 5.4 to v2 and A to v3. If the entered data is greater than the variable, it remains in the input stream. While entering string, blank spaces are not allowed. More than one variable can be used in cin statement to input data. Such operations are known as cascaded input operations.


For example,



where v1 and v2 are variables.


The operator >> accepts the data and assigns it to the memory location of the variables. Each variable requires >> operator. Both these statements should not be included in the bracket. The enter data is separated by space, tab, or enter. Similar to scanf() statement, cin does not require control strings such as %d for integer, %f for float, etc.


More examples

int weight;

cin>>weight // Reads integer value

float height;

cin>>height; // Reads float value

double volume;

cin>>volume; // Reads double value

char result[10];

cin>>result; // Reads char string

Output Streams

The output streams manage output of the stream, that is, display contents of variables on the screen. It uses << insertion operator before the variable name. It uses the cout object to perform console write operation. The syntax and example of cout statement are as follows.





cout<<v1 <<v2 <<v3 … <<vn;


where, v1, v2, and v3 are variables. The above statement displays the contents of these variables on the screen. The syntax rules are similar to cin. Here, the cout statement uses the insertion operator <<. The cout statement does not use the format specification such as %d, %f as used in C, etc. The cout statement allows us to use all ‘C’ escape sequences such as ‘\n’, ‘\t’, etc.


More examples

int weight;

cout<<weight // Displays integer value

float height;

cout<<height; // Displays float value

double volume;

cout<<volume; // Displays double value

char result[10];

cout<<result; // Reads char string

The table given below illustrates comparative programs on cout statements.


In this program, the cout statement displays the message “C PLUS PLUS” on the screen.

In this program, the string is first assigned to character pointer n. The cout statement displays the contents of variable n.

In this program, integer and float values are assigned to variables x and f. The cout statement displays the values of x and f.

3.5 Write a program to accept a string through the keyboard and display it on the screen. 
Use cin and cout statements.

Explanation: In the above program, cout statement displays a given string on the screen. It is similar to printf() statement. The cin statement reads data through the keyboard. It is similar to scanf() statement.

3.6 Write a program to read two integers and display them. Use cin and cout statements.

Explanation: In the above program, the cin statement reads two integers in variables num and num1. The cout statement displays the read numbers. The escape sequence ‘\t’ is used to insert tab between two numbers.

3.7 Write a program to display data using cout statements.

Explanation: Explanation of each of the cout statement is as follows.

  1. cout<<“================” – Displays line of on the screen.
  2. cout<<endl – Breaks a line.
  3. cout<<“Hello” – Display string “Hello” on the screen.
  4. cout<<“\n” – Breaks a line.
  5. cout<<123; – Displays integer 123.
  6. cout<<endl<<3.145 – Breaks a line and displays float number 3.145.
  7. cout<<endl<<“NUMBER: ”<<“\t”<<452 – Breaks a line, displays the string “number”, inserts a tab and integer number 452.
  8. cout<<“\n==== The end =====” – Displays the line and string.

3.8 Write a program to display int, float, char, and string using cout statement.

Explanation: In the above program, the statement cout<<“x = ”<<x <<“ y =”<<y <<“ z = ”<<z <<endl displays values of x, y, and z. The statement cout<<“City = ”<<city displays the contents of character array city.


3.9 Write a program to input int, float, char, and string using cin statement and display using cout statement.

Explanation: Explanation of the program is as follows.

  1. cout<<“\n Enter integer, float, and char” – Prompts message “Enter integer, float, and char”
  2.  cin>>x>>y>>z – Accepts integer, float, and char and stores in x, y, and z.
  3. cout<<“\n Enter a string: ” – Displays message “Enter a string: ”.
  4. cin>>city – Reads string through the keyboard and stores in the array city[15].
  5. cout<<“City = ”<<city; – Displays contents of the array city.

Test Your Skills Now!
Take a Quiz now
Reviewer Name