Title: cin (Keyboard Input) Statement
Course: CPS 120: Introduction to Computer Science ~ Master (Aug/2015) OLD > Section: Learning Units

cin Statement

- Used to read data from the standard input
- cin >> variable;
reads a data and stores it in variable of the same type
- cin.get(ch);
reads only a single character and stores it in ch variable
- getline(cin, str);
reads an entire line and stores it in str variable

Visual C++ console input (cin) statement

- Reads data from the console.
- Has the form:

cin >> [var];
OR
cin >> [var-1] >> [var -2] >> … >> [var -n];

Statement part | Description
cin | Console input command.
>> | Extraction operator; one precedes each datum to be read from the console. The >> indicates the flow of data.
var-n | Any declared variable.
; | End of statement.

To use cin, the following statements are needed near the top of a C++ program:
#include <iostream>
using namespace std;

- When the cin statement is executed, the program pauses until the user presses the Enter key.  A value is stored in the indicated variable.
- A cin statement is almost always preceded by a cout statement.

cin Statement Examples:

cin examples
cout << "Enter a score: ";
cin >> Score;

Prompts the user for a score, and then reads it from the keyboard and stores it in a variable named Score.
cout << "Enter a year (>1900): ";
cin >> Year;

Prompts the user for a year, and then reads it from the keyboard and stores it in a variable named Year.
cout << "Enter starting and ending days, separated by a space: ";
cin >> StartDay >> EndDay;

Reads two values from the keyboard and stores the first one in a variable named StartDay and the second one in a variable named EndDay.  The values must be separated by a space, a tab character or a newline.

This cin statement is the same as:
cin >> StartDay;
cin >> EndDay;

When the cin statement is executed and the user enters a value that matches the data type of the variable, good things happen. When the cin statement is executed and the user DOES NOT enter a value that matches the data type of the variable, BAD things MAY happen.

This table summarizes what happens when a cin statement is performed to read an expression from the keyboard into a variable.

Data type of variable to store expression in | Data type of expression entered by user at keyboard
 | int expression | double expression | string expression | char expression | bool expression
int variable | Stores int. | Truncates value and stores int. | Random value stored in int.* | Random value stored in int.* | Random value stored in int.*
double variable | Stores int as double. | Stores double. | Random value stored in double.* | Random value stored in double.* | Random value stored in double.*
string variable | Stores int as string (number 45 stored as string “45”). | Stores double as string (number 67.5 stored as string “67.5”). | Stores string. | Stores char as string (char ‘M’ stored as “M”). | Stores bool as string (bool true stored as “true”.
char variable | Stores first digit of int as char.  If single digit int entered, okay.  If multiple-digit int entered.* | Stores first digit of double as char.  If single digit double entered, okay.  If multiple-digit double entered.* | Stores first character of string as char.* | Stores char. | Stores first character of bool as char.*
bool variable | Random value stored in bool if int not 0 or 1.* | Random value stored in bool if double not 0 or 1.* | Random value stored in bool if string not 0 or 1.* | Random value stored in bool if char not 0 or 1.* | Stores bool.

* After the cin statement is performed, execution of the rest of the program is unpredictable.