Standard IO refers to input and output from the terminal, with no GUI. The standard namespace refers to our window. This is the usual launch point for learning how to program.
C
C++
C++ abstracts IO with streams. We have two standard lines. One is a header file, and the other sets our output namespace as the standard namespace.
The <<
operator outputs things, and the >>
operator takes things in. endl
will output a new line character, it flushes things out. A handy tip for the >>
and <<
operators:
- When we print, the arrows point towards the left
<<
. - When we input, the arrows point right
>>
. - The arrows point in the direction of the data.
We have helpful functions that let us detect and handle input failures:
cin.fail()
— returns true if any error occurs.cin
is some object with an error-state flag. Iffail()
is true, thencin.ignore()
will fail too.- So we need to
cin.clear()
before wecin.ignore()
.
- So we need to
cin.eof()
— returns true if the end of the file is reachedcin.clear()
will clear any error flags (if true).cin.ignore(int n, char ch)
— discardsn
characters or up to characterch
.
Within the iostream
library, we have a class called ostream
(for the output stream). cout
is an object of the ostream
class. The cerr
output stream is unbuffered.
JavaScript
The main way to print to the terminal is using console.log
.