Basic I/O
suggest changeThe standard library <iostream> defines a few streams for input and output:
| stream | description |
|---|---|
cin |
standard input stream |
cout |
standard output stream |
cerr |
standard error (output) stream |
clog |
standard logging (output) stream |
Of the four streams mentioned above, cin is used for input and the other three are used for output. In general, or in most coding environments, cin (console input, or standard input) is the keyboard and cout (console output, or standard output) is the monitor.
cin >> value
cin - input stream
'>>' - extraction operator
value - variable (destination)
cin here extracts the input entered by the user and stores it in the variable value. The value is extracted only after the user presses Enter.
cout << "Enter a value: "
cout - output stream
'<<' - insertion operator
"Enter a value: " - string to be displayed
cout here takes the string to be displayed and inserts it into the standard output, typically the monitor.
All four streams are in namespace std, so use them as std::cin, std::cout, std::cerr, and std::clog (or bring those names into scope).
The manipulator std::endl can be used only with output streams. It inserts a newline ('\n') and flushes the stream, so the output appears immediately. Flushing every line is often unnecessary; '\n' does not flush.
User input and standard output