Search⌘ K

Input and Output Functions

Explore how to perform input and output operations in C++ using streams. Learn about formatted input with the extraction operator, unformatted input methods, and output with the insertion operator. Understand handling built-in and user-defined data types as well as efficient string input for smooth data management.

Input

In C++ we can read from the input stream in two different ways: Formatted with the extractor >> and unformatted with explicit methods.

Formatted input

The extraction operator >>:

  • is predefined for all built-in types and strings.
  • can be implemented for user-defined data types.
  • can be configured by format specifiers.

🔑 std::cin ignores, by default, leading whitespace

#include <iostream>
//...
int a, b;
std::cout << "Two natural numbers:
...