Input

The built in IOStream library is used for input in C++. The object predefined for keyboard input in C++ is cin

cin is used for input. The operator used with cin is >>, which is called the extraction operator. The extraction operator extracts a value from the input stream and stores it in a variable. When the cin statement is executed, the program stops and waits for the user to type a value. When the user presses the Enter key, the cin object takes the value entered by the user and stores it in the variable listed in the cin statement.

Here are some things to remember when reading input:

  1. Only variables can be written in cin statements. You can never put a constant in a cin statement.
  2. The type of value entered must match the type of the variable in the cin statement. For an int, the value must be a whole number, which may include a plus or minus sign. For a char, the value must be a single character from the keyboard, but not a space. For a float, the value must be a real number, which may include a plus or minus sign, and can be in E notation.
  3. Each value can be read in a separate cin statement, or multiple values can be read in a single cin statement. For example, you can either write:
    	cin >> num1;           OR           cin >> num1 >> num2;
    	cin >> num2;
    
  4. If you enter a value that is too big to fit in the variable given, the value stored will be garbage. On our system, an int can only hold values between -32,767 and 32,767. If you need to hold a value bigger than that, use a long variable.
  5. When typing several input values, you can leave as many spaces, tabs, or blank lines between the different values. The >> operator ignores whitespace (our name for blank, tab, and newline characters) that comes before an input value.
  6. When input data is typed, it is stored in a special area of memory called a buffer. Then it is transferred to your variables. If you type more data than the program requests, it will wait in the buffer until another cin statement is executed. As data is transferred to your variable, the system keeps track of what part of the data in the buffer has been used up. If a cin is executed and the buffer has data, the program will take data from the buffer instead of waiting for you to type more data. If all data in the buffer has been used, then the program will stop and wait for you to type more data.
  7. When reading into a char variable, the program will use the next non-whitespace character. When reading into a numeric variable, the program will skip leading whitespace, read the numeric characters, and stop when it encounters an input value that is invalid for the type, such as a whitespace character or a non-numeric character.

Examples

int i, j;
char let;
float x;

Statement Input Contents After Input
cin >> i; 32 i = 32
cin >> i >> j; 11 40 i = 11, j = 40
cin >> i >> j; 1140 i = 1140, program waits for more input
cin >> i >> let >> x; 22 b 40 i = 22, let = 'b', x = 16.9
cin >> i >> let >> x; 22b40 i = 22, let = 'b', x = 16.9
cin >> i >> let >> j; 22 345 i = 22, let = '3', j = 45
cin >> i >> let; 22 3 45 i = 22, let = '3', 45 is saved for next cin
cin >> i >> j; 22 .5 i = 22, j = garbage
cin >> i >> x; 22 .5 i = 22, x = 0.5
cin >> i >> x; 22.5 i = 22, x = 0.5


Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2002