cin

  1. Data can be input from the keyboard and stored in a variable using cin and the extraction operator >>.

  2. Syntax:
    cin >> Variable_1 >> Variable_2 >> ... ;

  3. When a program is run and reaches a cin-statement, it stops and waits for input to be entered from the keyboard.

  4. It sets the first variable to the first value typed at the keyboard, the second variable equal to the second value, and so forth.

  5. Program does not read the input data until the enter or return key is pressed.

  6. More details of the input stream cin and the extraction operator >>:

    • when a user types on the keyboard and presses the enter key, the characters typed, including the enter key (same as the newline character) are stored temporarily in the input buffer stream. You can think of a stream as a sequence of characters.

    • There is an input buffer stream pointer which is initially pointing at the first character in the stream.

    • Every time a program wants to read data input from the keyboard, the program goes to the stream and reads one character at a time starting at the current pointer location.

    • After a character is read, the pointer moves to the next character.

    • Unless the program is reading a single character (of type char), reading from the stream is continued until encountering something inappropriate in the stream.

    • Leading white space characters (which consists of the blank, the tab key, or the newline character) are skipped.

    • In the statement:

      cin >> var;

      where var is a declared variable, the characters read are converted according to the encoding scheme to the type declared for var.