Console I/O

We can read from the keyboard and write to the console in C++ using input and output streams.

Table of Contents

  1. Input / Output Streams
  2. I/O Stream Libraries
  3. Outputting Text To the Console
  4. Console Output Example
  5. Outputting Variables to the Console
  6. Reading User Keyboard Input
  7. Reading String Data
  8. String Streams and File Streams
  9. Further Reading

Input / Output Streams

In C++ a stream is an abstract representation of a device on which input and output operations can be performed.

The devices which we can stream to and from are things like the operating system’s standard output (console), standard input (keyboard), and the file system.

You can think of a stream as a source or a destinations for a potentially unlimited sequence of bytes.

I/O Stream Libraries

Stream Class Hierarchy

From the hierarchy shown in the image, we typically use streams from the following built-in headers:

  • iostream - Standard Input / Output (Console & Keyboard)
  • fstream - File Input / Output
  • sstream - String I/O (Treat Strings as Streams)

Outputting Text To the Console

As we saw in our initial tour of C++ we can use std::cout with the “put to” operator << to write to the standard output stream, the console.

There is also a standard error and logging output stream, std::cerr and std::clog.

The std:: part means these streams are part of the “standard” namespace.

⏳ Wait For It:

Namespaces help prevent name conflicts in large projects. See namespaces section.

Console Output Example

#include <iostream>

int main() {
  // Let's output a string literal to the console:
  std::cout << "Hello World!\n";
  // Multiple insertion operations can also be chained together:
  std::cout << "This " << "is " << "it!" << "\n";
  // Use std::endl, instead of \n, to force the stream buffer to flush:
  std::cout << "Buffer Flush" << std::endl;
}

💡 Best Practice:

Using \n for newlines is said to be more performant that std::endl.

Outputting Variables to the Console

All built-in types have predefined ways to be output to a stream.

⏳ Wait For It:

In a later section we’ll define I/O stream operations for our own user-defined types.

Reading User Keyboard Input

The keyboard is an input stream that we can access using cin and the “get from” >> operator.

The stream will process the submitted data once the user presses enter. The newline character is automatically discarded.

⚡ Warning:

Streams can be “partially consumed” with the remaining data fed into the next stream.

Try inputting 2.3 for the number of ducks. The 2 will be streamed into the duck integer and the .3 will be streamed into the temperature double.

Reading String Data

We can read string data using cin, but the input will be whitespace terminated.

Assuming the answer variable is a string:

std::cin >> answer; // User inputs: Hello World
std::cout << answer; // Outputs: Hello

The getline function can be used to get an entire line of input, up to the newline character:

std::getline(std::cin, answer); // User inputs: Hello World
std::cout << answer;  // Outputs: Hello World

String Streams and File Streams

⏳ Wait For It:

In later sections, we learn how to stream to/from the file system and how to stream to/from RAM.

Streaming to data in and out of memory as a string is possible using the <sstream> header.

Streaming to and from files is done using the <fstream> header.

Further Reading