Input and Output operations are an important part of any program. Input operations take data from the user and output operations display the result of the program to the user.

Let us understand the basic input/output operations in C++ program with the help of a simple program.

The following program takes two numbers as input from the user, multiplies them and displays the result to the user.

//Program to multiply two numbers
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
 int num1,num2,result;
 cout<<"Enter the first number:\n";
 cin>>num1;
 cout<<"Enter the second number:\n";
 cin>>num2;
 result=num1*num2;
 cout<<"The product of the two numbers is "<<result;
 getch();
 return 0;
}

Here is a live preview of the program with the output.
Basic Input Output Operations in C++

First let us learn some important concepts about input/output in C++.

iostream header file

The standard C++ library has a header file called iostream. This file is included in all programs for performing input output operations.

cout and cin

cout and cin are objects that are declared in the iostream header file. cout (console output) corresponds to the standard output stream and cin (console input) corresponds to the standard input stream. A stream is basically a flow of data.

Insertion and extraction operators (>> and <<)

cout is used with the insertion/output operator (<<). This operator directs a value to standard output (eg. monitor).

cin is used along the extraction/input operator (>>).This operator gets a value from standard input (eg. keyboard)

Now let us see what’s happening in the program above.

cout<<"Enter the first number:\n";

In the above statement, the << operator directs the string “Enter the first number” to cout which then sends it to be displayed on the screen.

cin>>num1;

The input data received through the cin object is extracted by >> operator and stored in num1.

cout<<"Enter the second number:\n";
 cin>>num2;
 result=num1*num2;

Similarly user is asked to enter the second number ‘num2’.
Variable ‘result’ stores their product.

cout<<"The product of the two numbers is "<<result;

This statement prints the output on the screen. This statement is an example of what is known as cascading of operators.
In this statement, we have used the insertion operator twice. Once to print the string “The product of the two numbers is” and then to print the value of the variable ‘result’.

CASCADING :

This multiple use of insertion or extraction operator in a single statement is referred to as cascading of operators.
In the above program, for taking the input from the user, we could have cascaded extraction operator ‘>>’ to input both numbers in a single statement.

cout<<"Enter the two numbers\n";
cin>>num1>>num2;

Hope you are clear about basic input/output operations in C++ and the cascading of operators.
Do let us know your queries in comments below.



ABOUT THE AUTHOR:

Founder & Senior Editor of ChewCode.com and Editor at GizmoBolt.com. Sakshi is currently pursuing MCA. She is a foodie and has a unique love for learning computer programming languages. Follow her on Google + | Facebook | Twitter. Contact her at [email protected] for feedback and sending tips.


Tags: , , , , , ,

Leave A Response