Home » C++ » Input & Output Handling In C++ | Class 008 | C++ Complete Course

Input & Output Handling In C++ | Class 008 | C++ Complete Course3 min read

Input & Output Functions In C Plus Plus

by TechABU
Input & Output Functions In C++

We must take input and display the output to the user to write an effective program. C++ utilizes streams in sequential media like the screen or the keyboard for input and output operations. 

C++ provides a number of input and output functions for dealing with various sorts of input and output statements. All of the standard input and output stream objects are declared in the C++ program’s header files.

What Is Stream?

A stream is an object where a C++ program inserts characters to or extracts the characters from the stream.

  • Input Stream: The process is called input if the flow of bytes is from the device (for example, a keyboard) to the main memory.
  • Output Stream: The process is called output when the flow of bytes is in the reverse direction, i.e. from main memory to device (display screen).

In C++, the following header files are provided for Input/Output operations:

  1. iostream: The term “iostream” refers to a standard input-output stream. This header file provides object definitions such as cin, cout, cerr, and several others.
  2. iomanip: Input-output manipulators are abbreviated as iomanip. Stream manipulation is accomplished using the techniques specified in these files. Setw, setprecision, and other terms are defined in this file.
  3. fstream: The primary purpose of this header file is to describe the file stream. This header file manages data that is read from a file as input or written to a file as output.

In C++, the keywords cout and cin are frequently used to print outputs and take inputs. In C++, these are the most fundamental techniques for taking inputs and displaying output. To utilize cin and cout in C++, the header file iostream must be included in the program.

In this article, we will cover all the input/output functions that are widely used in C++ programs.

#1 – Standard output (cout):

The screen is a C++ program’s default standard output, and cout is the C++ stream object that specifies it. The insertion operator represented as (<<) is used in conjunction with cout. Consider the following code segment:

#include <iostream>
#include <conio.h>
using namespace std;
int main ()


	cout <<" Learn C++ Programming " <<endl;	//displays output on the screen
	cout << " I love Coding " <<endl;	    	//displays output on the screen

	getch ();
	return 0;


In the above C++ program, the data is inserted into the stream using the insertion operator (<<). It inserts the string “Learn C++ Programming” and “I love Coding” into the standard output stream cout.

In a single statement, the insertion operator (<<) can be used several times. Take a look at the following line of code.

For Example:

cout << "Welcome to " << " TechABU’s " << " Programming Course " ;

The above statement displays the message “Welcome to TechABU’s Programming Course.” When printing more than one variable or a combination of variables and constants, the insertion operator (<<) must be repeated.

#2 – Standard input (cin):

The keyboard is the most common method of inputting data into a computer. To handle standard input, C++ utilizes the stream extraction operator (>>) and the object cin. The variable that will store the data that will be extracted from the stream must be preceded by the operator (>>)

Consider the following C Plus Plus Program.

#include <iostream>
#include <conio.h>
using namespace std;
int main ()


	int a, b, sum;
cout << " Enter a Value for a = " ;			//displays output on the screen
cout << " Enter a Value for b = " ;			//displays output on the screen
	sum = a+b;
	cout << " The Sum of a & b = " <<sum;

	getch ();
	return 0;


In the above example, int a and int b declare an integer variable, and the second statement waits for cin’s input before storing it in this integer variable.

Once the enter key is pressed, the object cin will process keyboard input. As a result, even if you just request a single character, the extraction from cin will not process the input until after the character has been introduced and the user hits enter.

#3 – Un-buffered standard error stream (cerr)

The standard error stream (cerr) in C++ is used to output errors. This is a member of the ostream class as well. Because cerr is unbuffered in C++, it is used when the error message does not need to be displayed immediately. It lacks a buffer that would allow it to save the error message and display it later.

The key difference between cerr and cout is that when you use “cout,” the output is redirected to a file. However, when you use “cerr,” the error is not recorded in a file. (This is what the term “un-buffered” refers to. It is unable to save the message.)

#4 – buffered standard error stream (clog)

This is an instance of the ostream class that is used to show errors. However, unlike cerr, the error is first entered into a buffer and stored there until the buffer is not filled. If the buffer hasn’t been cleaned directly (using flush()). The error notice will also be shown on the screen.

Most Common Built-in Input/Output Functions In C++

1. gets () function

gets () is a standard library function that gets a string from the keyboard and is defined in the studio.h header file. It has the following general syntax:

gets ( string variable name );

It takes characters from stdin and stores them as a string in the ‘string variable’ until it reaches a newline character or the end of the file.

2. puts () function

The puts () function prints a string to the screen and automatically inserts a newline character. The puts () function has the following general syntax:

puts ( string variable );

3. getch () function

The getch () function is a console function that retrieves the next available keystroke or character. Nothing is repeated on the screen as a consequence of this function. The function waits until a key is pushed if no keystroke is available. When a key is pressed, the function returns the value of the keystroke. 

It has the following general syntax:

getch ();

Related Articles

Leave a Comment