Home C++ Tutorial C++ Basic Input/Output

C++ Basic Input/Output

by Anup Maurya
37 minutes read

In this tutorial, we will learn how to take input from user and display the output to user with the help of examples.

C++ Output

In C++, we use cout to sent formatted output to standard output devices, such as the screen . We use the cout object along with the << ( insertion operator ) for displaying output. The insertion operator << also called the put to operator directs the information on its right to the object on its left.

The cout object can be used to display individual characters, strings and even numbers. It is predefined object that refers to the standard output stream. The cout object, whose properties are defined in <iostream> represents that stream.

#include <iostream>
using namespace std;

int main() {
    // prints the string enclosed in double quotes
    cout << "How to print output on output deveices.";
    return 0;
}

Output

How to print output on output deveices.

How does this program work?

  • In first line of program we have included the iostream header file that allows us to display output.
  • Namespaces are used to organize code into logical groups and to prevent name collisions that can occur especially when your code base includes multiple libraries .The cout object is defined inside the std namespace. To use the std namespace, we used the using namespace std; statement.
  • Every C++ program starts with the main() function, that’s why it’s called as entry point of any program in C++.
  • cout is an object that prints the string inside quotation marks " " as it is. It goes along with the << operator.
  • return 0; is the “exit status” of the main() function. The program ends with this statement, however, this statement is not mandatory.
#include <iostream>

int main() {
    // prints the string enclosed in double quotes
    std::cout << "How to print output on output deveices.";
    return 0;
}

Note: If we don’t include the using namespace std; statement, we need to use std::cout instead of cout.

C++ Input

In C++,we use cin takes formatted input from standard input devices such as the keyboard. We use the cin object along with the >> (extraction operator) for taking input. The extraction operator >> also called the get from operator directs the information on its left to the object on its right.

#include <iostream>
using namespace std;

int main() {
    int num;
    cout<<"Enter a number of our choice:";
    cin>>num;
    cout<<"You have entered:"<<num;
    return 0;
}

Output

 Enter a number of our choice:34
 You have entered:34

Cascading Of Input/Output Operator

Insertion and extraction, more the one time in single statement is known as Cascading of Input/Ouput Operator.

#include <iostream>
using namespace std;

int main() {
    int numOne, numTwo;

    cout << "Enter two integer numbers: ";
    cin >> numOne >> numTwo; //cascading of extration operator

    cout << "Number One is : " << numOne << endl; //cascading of insertion operator
    cout << "Number Two is :" << numTwo; 
    cout << "Average of two given number is :"<< (numOne+numTwo)/2;
    return 0;
}

Output

Enter two integer numbers:28
24
Number One is :28
Number Two is :24
Average of two given number is : 26

related posts

Leave a Comment

Enable Notifications OK No thanks