Using C++: Part 1: Console Input and Output

Learning C++

Using C++


Console Input and Output

C++ arose during a period of time when computers did not have complex graphical visual interfaces. Instead, input and output existed as a series of text on a screen. Typing and processing software took place through the console, an interface for commands based on processing them line by line. (This is often called command-line interfaces.)

<iostream>

One of the standard libraries included in most versions of C++ is the iostream, library. Standing for Input and Output Streams, this library includes functionality for common actions working with the console.

cin and cout

Based on the previous language, C, the keywords cin (C-Input) and cout (C-Output) work in C++. They define streams, a one-way channel of data.

In most early operating systems, data was managed through streams by connecting one software package to another. Data would then be sent along one channel and received along another.

By default, cin and cout default to the console. This means that they can be used to “put” or “get” data from the command-line.

 Stream Operators

Two special operators exist for working with streams, << (put) and >> (get). These two less-than or greater-than signs point to the keyword they most commonly work with in regard to streams.

Working with Lines and Special Characters

Working with cin was designed to process letters and numbers. It considers things like spaces and other special characters to be the end of input. This most commonly shows up when using it with input that uses a space.

For input that extends until it runs into the new-line character, the function getline() can be used. It takes all input, regardless of character type, until it runs into a special character represented as a backslash and the letter n : “\n”.

Play with the example on Repl.it!