×

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++ find missing in the second array

Given two arrays A and B of sizes n and m. Find the elements from array A that are not present in array B. Example Input : a[] = {1, 2, 3, 5,...

3 minutes read.

Processing strings using std string stream in C++

A string class object called std: string stream is used to streamline into various variables, just as files can pour into strings. This class's things make use of a string...

3 minutes read.

How to initialize a dynamic array in C++

Regular arrays or static arrays have a predetermined size or fixed size. Change in the size of regular arrays is not possible. The memory size for static arrays determines at compile...

4 minutes read.

How to Handle Divide by Zero Exception in C++

If you are a programmer or interested in coding then it is obvious that you face some illogical test cases. Suppose, you have written one program that calculates the factorial...

6 minutes read.

C++ Functions

In other programming languages, a function is referred to as a process or a subroutine. We can design functions to execute any task. A function can be used repeatedly. It...

5 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.

ATM machine program in C++ using functions

Automated Teller Machines (ATMs) carry out daily financial transactions. They are straightforward and simple, allowing customers to complete self-service transactions quickly. ATMs can then be used to withdraw cash, deposit...

3 minutes read.

Advantage and disadvantage friend function C++

Friend Function: - A friend function in C++ is a function that can access the private, protected, and public members of a class. In C++, a friend function is a function that...

2 minutes read.

Check for Balanced Brackets in an Expression (well-formedness) using Stack

Write a program that check the correctness of the pairs and ordering of the characters “{“, “}”, “(“, “)”, “[“, “]” in the expression string exp. Example: Checking for balanced parenthesis is one of...

2 minutes read.

Returning Multiple Values from a Function using Tuple and Pair in C++

We may come across many situations where after the driver code's execution is performed in a code block, the return should be either multiple values or a single value possibly...

4 minutes read.

Dynamic Memory Allocation in C++

In some programming situations, the number of data items changes as the program is running, which is known as dynamic data or input. Consider a real-world situation where a program...

3 minutes read.

How to Reverse a String in C++ using For Loop

For Loop: We may loop through a certain section of C++ code repeatedly using the for loop. A for loop is carried out if the test expression yields a true result. The...

4 minutes read.

Difference between Exit and Return

Define Exit() At the point when a client needs to leave a program from this capability is utilized. A void return type capability calls all capabilities enrolled at the exit and ends...

3 minutes read.

C++ Tricks for Competitive Programming

If you are interested in Computer science or Information technology, you must have heard about competitive programming. Competitive programming is a way to improve your problem-solving skills. There are various...

7 minutes read.

C++ Bidirectional Iterators

Iterators : Iterators serve as a link between algorithms and STL containers, allowing the data inside the container to be modified. They let you to iterate through the container, access and...

3 minutes read.

Reverse a String using Stack C/C++

Reverse the given string using stack. To turn "tutorialandexample" into "elpmaxednalairotut," for instance. Here is a straightforward stack-based technique for reversing strings. Algorithm: 1) Make a stack that is empty. 2) Push each character...

3 minutes read.

Add two numbers represented by two arrays in C++

The array stores a number in such a way that each digit of the number is represented by an array element. As an example, The array number 147 is 1,4,7. To add...

3 minutes read.

C++ Variable

In this article, we will discuss variables in C++ with their types and examples. What are Variables? Variables are specific memory storage spaces that hold a value. During the execution of a...

4 minutes read.

C++ Program to find the element that occurs once

Write a program to find the element in the array that occurs once. Given that all the numbers in the array are present two times and the array is sorted,...

7 minutes read.

Remove duplicates from sorted array in C++

Remove duplicates from the sorted array in C++. This same process, due to a sorted array, is to erase the redundant components from the array. Examples: Input  : arr[] = {2, 2, 2,...

4 minutes read.