Cout : Display Output to User in C++ (Insertion Operator in C++)



Cout : Display Output to User Using Screen(Monitor)

In C++ Insertion operator is used to display value to the user. The value may be some message in the form of string or variable.

Syntax : Display Value to User

cout << variable;

Explanation : Insertion Operator

  • Include <iostream.h> header file to use cin.
#include<iostream.h>//traditional C++
OR
#include<iostream>// ANSI Standard
using namespace std;
  • cout is used for displaying data on the screen.
  • The operator << called as insertion operator or put to operator.
  • The Insertion operator can be overloaded.
  • Insertion operator is similar to the printf() operation in C.
  • cout is the object of ostream class.
  • Data flow direction is from variable to output device.
  • We can still use printf() for displaying an output..
  • Multiple outputs can be displayed using cout.

Live Example : Displaying Output on Screen

#include<iostream.h>
using namespace std;
int main()
{
    int number1;
    int number2;
    cout<<"Enter First Number: ";
    /* Display the message to tell the 
          user to do appropriate action */
    cin>>number1;
    cout<<"Enter Second Number: ";
    /* Display the message to tell the 
          user to do appropriate action */
    cin>>number1;
    cin>>number2;
    cout<<"Addition : ";
    cout<<number1+number2;  //Display Result
    return 0;
}

Cout Operator in C++ Insertion Operator

Output :

Enter First Number: 8
Enter Second Number: 8
Addition : 16

Different ways of using cout in c++

Way 1. Simple Use of cout and Insertion Operator:

int tempNumber=6;
cout << tempNumber;
  • In above example, tempNumber is declared as integer variable. And value 6 is assigned to it.
  • when control comes over cout , Program will display the output to screen.
  • Likewise we can display integer, character or string.

Way 2 : Cascading Multiple Variables in Cout

int a=10;
int b=20;
cout << a << b;

is similar to -

int a=10;
int b=20;
cout << a;
cout << b;

Way 3 : cout to display string

cout << string ; //for displaying string output
or
cout << "Display String" ;
  • The extraction operator is also used for getting string.
  • When blank space is detected, extraction operator stops reading input.
  • cin allow us to enter only one word.
  • For reading entire line, we use getline() function.

Way 4 : Use of expression in cout

int num1;
int num2;
cin >> num1;
cin >> num2;
cout<< "Addition is : " << num1+num2 ;
  • Expression can be evaluated inside cout statement.
  • In above example, we are accepting 2 values from user.
  • The addition of two numbers is displayed on the screen
  • For that we use the expression for adding two numbers in the output statement directly.

Way 5 : Display Output on new line

  • We can use ‘n‘ or ‘endl‘ to print the output on new line.
cout << "Hello,n";
cout << "My name is nPooja...";
or
cout << "Hello," <<endl;
cout << "My name is" <<endl;
cout << "Pooja..." <<endl;

Output :

Hello,
My name is
Pooja...

*External Link : More on cout | cout Statement | Sample Example