×

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:

Iostream in C++

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:

Iostream in C++

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:

Iostream in C++

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:

Iostream in C++

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.


Related Topics

C++ Switch

In C++, an expression or variable can be tested against a range of constant values using a switch statement, which is a control flow statement. It offers a productive method...

4 minutes read.

C++ Encapsulation

The following two essential components are present in all C++ programmes: Functions are the parts of a programme that perform actions, and they are termed programme statements (code).Program data is the...

4 minutes read.

Two dimension array

C++ Two dimension (2D) Array Two dimension (2D) array is an array of arrays. It is represented in the form of row and column. The elements of 2D array are accessed through the...

2 minutes read.

Structure Vs Class in C++

The structure in C++ is similar to that of a class, with a few exceptions. Both the structure and the stage, the most important thing is safety. The property is...

4 minutes read.

Upcasting and Downcasting in C++

With the help of various examples in the C++ programming language, this section will cover Upcasting and Downcasting. Upcasting and downcasting, on the other hand, are two forms of object typecasting. Consider...

3 minutes read.

abs() function in C++

In C++, the abs() function returns the absolute value of any integer number. Using this function a negative integer is multiplied by -1 and positive number or zero is returned...

4 minutes read.

Const_cast in C++ Type Casting Operators

In this tutorial, we will learn enough about const cast in the most widely used programming language, C++, as well as type casting operations. C++ supports the four types of casting...

4 minutes read.

Hello World Program in C++

The steps for “Hello World” C++ program are as follows: Write a C++ code given below in an editor. Save the file with .cpp Compile the code using C++ compiler or using online...

3 minutes read.

Armstrong Number using While Loop in C++

What is while Loop? A while loop or while statement repeats all code of its body as long as a specific condition is satisfied. The loop ends if or when the...

4 minutes read.

Accumulate() and partial_sum() in C++ STL Numeric header

The C++ STL's numeric library includes the numeric header. This library provides efficient numeric arrays, support for random number generation, and fundamental mathematical operations and types. Several of the numeric...

3 minutes read.

Const Keyword in C++

The const programming language keyword will be covered in this section. The constant value that cannot change during program execution is defined using the const keywords. It implies that once...

9 minutes read.

sort() function in C++

This tutorial covers the various built-in sort functions found in the C++ algorithm’s library.  What Does C++ Sort Mean? The concept of sorting in C++ entails rearranging an array's elements in a...

3 minutes read.

Armstrong Number Program in C++

Let's first define Armstrong number before writing the C++ program to check whether the number is Armstrong or not. The sum of the cubes of its digits is equal to the...

2 minutes read.

Print Table Using For-loop in C++

Multiplication Table In mathematics, a table is created by multiplying a certain number by all of the counting numbers, i.e., 1, 2, 3, 4, 5, 6, and so on. It is...

3 minutes read.

Types of polymorphism in C++

What is polymorphism in C++ ? Polymorphism literally translates to "multiple forms". This indicates that the same thing behaves differently depending on the context in programming. Polymorphism is a feature of C++...

6 minutes read.

C++ vs C#

What exactly is C++ programming? Bjorne Stroustrup is the creator of the C++ programming language. His goal was to create a powerful object-oriented programming language with the capabilities of C. It...

4 minutes read.

How to Use Getline in C++

What is getline in C++? Similar to the cin function, which allows the programmer to take input from the user getline() function also takes input from the user. But in this...

4 minutes read.

Constructor Overloading

The program contains more than one constructor in a class with the same name, and different types of arguments are called constructor overloading. Calling of constructor depends on the number and types...

2 minutes read.

Object Slicing in C++

In this article, we will learn about Object slicing. When an object from a derived class is assigned to an object from a base class in C++, these extra attributes...

3 minutes read.

Virtual class in C++

Introduction to Virtual Base class Virtual base classes can be utilized in virtual inheritance as a mechanism for examining many "instances" of a certain class while searching through multiple inheritances in...

6 minutes read.