Console I/O
We can read from the keyboard and write to the console in C++ using input and output streams.
Table of Contents
- Input / Output Streams
- I/O Stream Libraries
- Outputting Text To the Console
- Console Output Example
- Outputting Variables to the Console
- Reading User Keyboard Input
- Reading String Data
- String Streams and File Streams
- 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
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.
#include <iostream>
int main() {
const double pi{ 3.1415926 };
int answer{ 42 };
long double hugeNumber{ 1.5e300 };
std::cout << pi << "\n";
std::cout << hugeNumber << "\n";
std::cout << "The answer is " << answer << ".\n";
}
⏳ 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.
#include <iostream>
#include <cmath>
int main() {
// Reading in an integer.
int ducks;
std::cout << "How many ducks do you see: ";
std::cin >> ducks;
std::cout << "There are " << ducks << " ducks.\n";
// Reading in a double.
double temperature;
std::cout << "What is the temperature: ";
std::cin >> temperature;
std::cout << "The temperature is " << temperature << ".\n";
}
If you wish to try this program on Compiler Explore, you will need to find and click the “Execution stdin” button found in the right-hand output tab. That will enable a place to type input.
⚡ 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.