C++ Basic Input and Output

In this tutorial we learn about basic input and output capabilities provided by C++ programming language. For Input and output, C++ programming language uses an abstraction called streams which are sequences of bytes. A stream is an abstract entity where a program can either write or read characters. It hides the hardware implementation and details from the C++ program.

In any input operation, data from mouse, keyboard, external disk etc to main memory for program consumption whereas data flows from main memory to output devices like screen, printer etc, in any output operation. In most programming environments, standard input device by default is keyboard and standard output device by default is screen. The C++ standard library defines some stream objects for input and output.

Standard Input Stream (cin)

cin is a predefined object of class istream. cin object by default is attached to the standard input device which is keybord in most programming environments. cin along with extraction operator (>>) is used to take keyboard input from user. Extraction operator is followed by a variable where the input data is stored. cin is an input statement, hence programs waits for user to enter input from keyboard and press enter. Input data flows directly from keyboard to variable.

Here is an example of reading an integer value from user
int count;
cin >> count;
Here is an example of reading an integer value from user
int count, sum;
cin >> count >> sum;

Based on the data type of the variable after extraction operator(>>) cin determine how it interprets the characters read from the input. If the variable is of integer data type than it will expect integer input from user.
We can also take string input from user using cin and extraction operator. For string input, it reads characters till first white space character(space, tab, newline etc). With cin we can only read one word at a time, it does not support reading an entire line having space characters. To read an entire space seperated sentence we should use "getline" function.

Standard Output Stream (cout)

cout is a predefined object of class ostream. cout object by default is attached to the standard output device which is screen in most programming environments. cout along with insertion operator (<<) is used to display data to user on screen. Insertion operator is followed by a variable or constant which needs to be displayed to user. Input data flows directly from variable to output stream.

Here is an example of printing an integer on screen
int count = 10;
cout << count;
We can print multiple values at a time by cascading insertion operator.
cout << count << 20 << "Hello";
We can use ‘\n‘ or endl to print the output on new line.
cout << 20 << endl << "Hello\n" << "World";
Output :

C++ Program for Basic Input Output using cin and cout

#include <iostream>
using namespace std;
int main( ) {
   char name[50];
   int age, salary, account_number;
   cout << "Enter your name \n";
   // reading string 
   cin >> name;
   cout << "Enter your age \n";
   // reading integer
   cin >> age;
   cout << "Enter your salary and account number\n";
   // taking two input at a time 
   cin >> salary >> account_number;
   cout << name << endl << age << endl <<
        salary << endl << account_number;
   return 0;
Enter your name
Enter your age
Enter your salary and account number
2345 87654
In above program, we are taking input from user using cin and printing it on screen using cout as explained above. We are taking input of different data types like integer and string. We are also taking multiple inputs in a single cin statement by using multiple extraction operator.