Iostream in C++
Using Iostream in C++, we can perform input and output operation capabilities. This represents input and output, and the stream is used to carry out this capability. A stream is a collection of characters or bytes that is used to perform IO operations in C++.
In programming, the language stream holds the destination's address. This represents the input and output stream.
Syntax:
The syntax for the input and output streams is shown below.
1. Source Stream:
We refer to the input stream in C++ as "cin," which is an instance of the stream class. In C++, the cin keyword is used before the>> operator. View the syntax below.
Example:
Cin>>variable_name;
2. Exit Stream:
The output stream function in C++ is called "cout," an instance of the stream class. The cout keyword is used before the operator in C++. View the syntax below.
Example:
Cout << variable_name
How does C++'s stream operate?
Right now, we know that an input-output stream in a computer language is known as an iostream. To receive user input and print the value on the console in C++, we utilize the functions cout and cin. We will go into more detail about these two operations in this section. View below
1. Input stream: This procedure is referred to as an input stream if a series of characters or bytes passes from the device to the memory. Consider the keyboard. This implies that we are giving the system input but cannot view it.
2. Output stream: The process is referred to as the output stream if a sequence of characters or bytes flows or processes in the opposite direction.Afterward, the procedure is referred to as an output stream in a programming language, such as ScreenScreen. We can see anything on the device's ScreenScreen as it flows from the main memory in this instance. The primary purpose of this stream is to display output on the screen.
Iostream operations in C++
The iostream header contains information on all these IO operations. This header includes the variables cin and cout as well. Let's talk about the operations, or capability, that this header file, stream, provides, along with a single syntax for using them in programming.
1. cin (stream - standard input stream)
In a computer language, an input stream is processed using the stream class, in which cin is an instance. This function allows us to accept the input arguments and either process them or give the variable a value. We follow the variable name with the '>>' operator to use this. To better comprehend it, let's use an illustration; we must have stream included as a program header to utilize this function; otherwise, we will encounter an error.
#include <iostream>
int main ()
{
int rollnumber;
cin>>rollnumber;
return 0;
}
The cin function from the stream is used in the example above to accept the input before the operation>> operator.
2. cout (stream - standard output stream)
The output stream is processed using the cout instance of the stream class, which is a feature of the C++ programming language. Using this function, we may view the results of the parameters we have supplied. We must use the “” operator with it, followed by the variable name, to use this. To better comprehend it, let's use one example. We must include stream as a program header to utilize this function; otherwise, we will encounter an error.
#include <iostream>
using namespace std;
int main()
{
cout << "Message to show on screen!!";
return 0;
}
The cout function is used in the example above, followed by the operator, to print the message on the console.
3. clog
This stream function may be found in the stream header file and is used to display errors that have occurred.
For a better grasp of its grammar, please view the sample below;
#include <iostream>
int main()
{
clog << "This is used to show the error in io stream.!!";
return 0;
}
Examples of streams in C++
The examples of C++ iostream are shown below:
Example 1:
In this example, the value entered by the user is obtained using the 'cin' function from iostream.
#include <iostream>
using namespace std;
int main()
{
int rollnumber;
cout << "Demo for CIN function in iostream";
cout << "ask user to enter the age here :::";
cin>>rollnumber;
cout << "\nit will print the roll number here " <<rollnumber;
return 0;
}
Output:

Example 2:
To output the user values to the console or ScreenScreen in this example, we are utilizing the 'cout' function from iostream.
#include <iostream>
using namespace std;
int main()
{
cout << "Demo for COUT function in the stream";
cout << "cout followed by the << operator!!";
cout << "end of the program!!";
return 0;
}
Output:

Example 3:
Another function from the iostream header file is used in this example to manage the input and output streams in C++.
#include <iostream>
using namespace std;
int main()
{
color<< "This function is used to print the error !!";
return 0;
}
Output:

Example 4:
The error function, also present in the stream file, is used in this example to trace any errors that may have happened.
#include <iostream>
using namespace std;
int main()
{
clog << "This function is used to print the error when occurred in the program!!";
return 0;
}
Output:

We can handle the input and output stream in C++ by using a number of the functions that Iostream offers. Numerous parts, including cin, cout, cin, and many more, are included in this stream header file. This allows us to read input, print it, and, if necessary, track down errors. However, we must include the iostream file in our program to deal with these functions. They offer us many parts for error, each with slight variations.