Skip to main content

3.1) Reading From A Stream


Examples

cout << "Enter size: ";
cin >> size;
cout << "Enter x and y coordinates: ";
cin >> x >> y;

Explanation

  • cin is the counterpart to cout; it is an input stream (an istream object) representing console input - by default, the keyboard
  • >> is the stream extractor operator
    • The istream object appears on the left
    • The variable to hold the extracted data appears on the right
    • Tip: think of the >> as an arrow showing direction of data flow
  • The stream extraction operator works with all of the primitive types and with strings
  • Extractions can be chained together to extract items of data input multiple variables; the items of data must be separated by whitespace (spaces, tabs, newline)