Input and Output Operators in C++
Operators
Input and output operations in C++ are carried out using streams, simply a series of bytes.
Input Stream: This process is referred to as input if the direction of the bytes' flow is from the device (for instance, the keyboard) to the main memory.
Output Stream: This process is referred to as output if the direction of the bytes' flow is the opposite, i.e., from main memory to the device (display screen).
- The standard input and output streams are interacted with using input and output operators in C++.
- With the help of these operators, you can show data on the console and read data from the user (input).
- Typically, std::cin and std::cout represent the standard input and output streams, respectively.
- The libraries that come with C++ give us a variety of options for conducting input and output.
Iostream
The C++ header file for the "input-output stream" is called "iostream". It offers classes and objects to carry out input and output operations and is a crucial component of the C++ Standard Library.
- It is simple to interact with the user and display results on the console thanks to the usage of the iostream library for console-based input and output.
In the iostream header, two classes are primarily defined:
std::cin :-
- The standard input stream is represented by the cin (console input) object or std::cin.
- It is employed to read information from the user via the console.
- The input operator >> can be used to extract data from cin.
std::cout :-
- The standard output stream is represented by the cout (console output) object, or std::cout.
- Data is displayed on the console using it.
- The output operator << can be used to add data to the cout for output.
To use the features of the iostream library, don't forget to include the iostream> header at the start of your C++ programs. Additionally, you can access cin and cout without having to prefix them with std:: by using the namespace std; statement.
Input Operator (>>):
- Data can be retrieved from the standard input stream (std::cin) using the input operator (>>).
- It enables you to read many data types from the user and store them in variables, including integers, floating-point numbers, characters, and strings.
Let's read an integer from the user and put it in a variable called number as an illustration.
#include <iostream>
int main() {
int number;
std::cout << "Enter an integer: ";
std::cin >> number;
std::cout << "You entered: " << number << std::endl;
return 0;
}
Output:
![Input and Output Operators in C++](https://static.tutorialandexample.com/cpp/input-and-output-operators-in-cpp1.png)
The user is prompted to enter an integer when the program is running. The number variable is used to hold the entered value before using the output operator () to display it on the console.
Taking multiple inputs:
#include <iostream>
using namespace std;
int main() {
char a;
int num;
cout << "Enter a character and an integer: ";
cin >> a >> num;
cout << "Character: " << a << endl;
cout << "Number: " << num;
return 0;
}
Output:
![Input and Output Operators in C++](https://static.tutorialandexample.com/cpp/input-and-output-operators-in-cpp2.png)
Output operator:
- Data can be displayed to the standard output stream (std::cout) using the output operator ().
- It enables you to print a variety of data to the console, including integers, floating-point numbers, characters, and strings.
Let's display a message and a variable's value to the console as an illustration:
#include <iostream>
int main() {
int age = 30;
std::cout << "My age is: " << age << " years old." << std::endl;
return 0;
}
Output:
![Input and Output Operators in C++](https://static.tutorialandexample.com/cpp/input-and-output-operators-in-cpp3.png)
This example prints the message "My age is:" to the console, then prints the value of the age variable and the phrase "years old."
To display numerous elements on a single line, you can chain several output processes together.
#include <iostream>
int main() {
int num1 = 10, num2 = 20;
std::cout << "The numbers are: " << num1 << " and " << num2 << std::endl;
return 0;
}
Output:
![Input and Output Operators in C++](https://static.tutorialandexample.com/cpp/input-and-output-operators-in-cpp4.png)
- Basic console-based interactions in C++ require the input (>>) and output () operators.
- When a program is running, they offer a quick and easy way to interact with the user and show information on the console.
A number of header files are provided in C++ to execute input and output (I/O) operations. Here are a few of the more typical ones:
1. <iostream>: The most typical header for conventional input and output operations. The classes'std::cin' and'std::cout' allows users to read input from the console and print output to the console, respectively.
2. <fstream>: I/O operations on files utilize this header. For reading and writing to files, it offers classes like'std::ifstream' and'std::ofstream'.
3. <sstream>: I/O operations involving string streams require this header. It offers classes such as'std::istringstream' for reading from strings and'std::ostringstream' for writing to strings.
4. <iomanip>: This header offers formatting options for the output, including the ability to control the width, precision, and alignment of the data printed to the console or a file.
5 .<cstdio>: The C-style I/O header, or "cstdio," offers features like printf and scanf. Although it isn't a part of the C++ Standard Library, C++ programs can utilize it.
- The tools required to handle various input and output operations in C++ are provided by these headers.
- To utilize their features, don't forget to include the necessary header(s) at the start of your C++ program.
For instance:
#include <iostream> // For console input and output
#include <fstream> // For file input and output
#include <sstream> // For string stream input and output
#include <iomanip> // For formatting output
#include <cstdio> // For C-style I/O functions (optional in C++ programs)