Professional Documents
Culture Documents
Istream Member Functions
Istream Member Functions
• This statement is called the input statement. The purpose of input statement is to read and store
values in a memory location.
• With the help of syntax of cin, computer can get the data from the keyboard.
• In C++, >> is called the stream extraction operator. This operator is a binary operator and thus takes
two operands.
• The extraction operator skips all leading whitespace characters when scanning for the next input
value.
• A single input statement can read more than one data item by using the operator >> several times.
Every occurrence of >> extract the next data item from the input stream.
cin and the extraction operator
• Example:
cin >> a >> b; single input statement
cin >> a;
cin >> b;
cin and the extraction operator
• When the extraction operator >> scanning the next data, then it skips all whitespace
characters.
• To read data into a char variable, after skipping any leading whitespace characters, the
extraction operator >> find and stores only the next character; reading stops after a single
character.
• To read data into an int or double variable, after skipping all leading whitespace characters
and reading the plus or minus sign (if any), the extraction operator >> reads the digits of the
number, including the decimal point for the floating-point variables and stops when it finds a
whitespace characters or a character other than a digit.
cin and the extraction operator
int a;
Statement Input Values stored in memory
double z;
char ch, ch1, ch2; cin >> ch; A ch = ‘A’
cin and the extraction operator
int a;
Statement Input Values stored in memory
double z;
char ch, ch1, ch2; cin >> ch; A ch = ‘A’
cin >> ch; AB ch = ‘A’, ‘B’ is held for later input
cin and the extraction operator
int a;
Statement Input Values stored in memory
double z;
char ch, ch1, ch2; cin >> ch; A ch = ‘A’
cin >> ch; AB ch = ‘A’, ‘B’ is held for later input
cin >> a; 45 a = 45
cin and the extraction operator
int a;
Statement Input Values stored in memory
double z;
char ch, ch1, ch2; cin >> ch; A ch = ‘A’
cin >> ch; AB ch = ‘A’, ‘B’ is held for later input
cin >> a; 45 a = 45
cin >> a; 45.74 a= 45, .74 is held for later input
cin and the extraction operator
int a;
Statement Input Values stored in memory
double z;
char ch, ch1, ch2; cin >> ch; A ch = ‘A’
cin >> ch; AB ch = ‘A’, ‘B’ is held for later input
cin >> a; 45 a = 45
cin >> a; 45.74 a= 45, .74 is held for later input
cin >> z; 45.74 z = 45.74
cin and the extraction operator
int a;
Statement Input Values stored in memory
double z;
char ch, ch1, ch2; cin >> ch; A ch = ‘A’
cin >> ch; AB ch = ‘A’, ‘B’ is held for later input
cin >> a; 45 a = 45
cin >> a; 45.74 a= 45, .74 is held for later input
cin >> z; 45.74 z = 45.74
cin >> z; 45 z = 45.0
cin and the extraction operator
int a;
Statement Input Values stored in memory
double z;
char ch, ch1, ch2; cin >> ch; A ch = ‘A’
cin >> ch; AB ch = ‘A’, ‘B’ is held for later input
cin >> a; 45 a = 45
cin >> a; 45.74 a= 45, .74 is held for later input
cin >> z; 45.74 z = 45.74
cin >> z; 45 z = 45.0
cin >> a >> z; 45.74 a= 45, z = .74
cin and the extraction operator
int a;
Statement Input Values stored in memory
double z;
char ch, ch1, ch2; cin >> ch; A ch = ‘A’
cin >> ch; AB ch = ‘A’, ‘B’ is held for later input
cin >> a; 45 a = 45
cin >> a; 45.74 a= 45, .74 is held for later input
cin >> z; 45.74 z = 45.74
cin >> z; 45 z = 45.0
cin >> a >> z; 45.74 a= 45, z = .74
cin >> z >> a; 45.74 z= 45.74, computer waits for the input value for a
cin and the extraction operator
int a;
Statement Input Values stored in memory
double z;
char ch, ch1, ch2; cin >> ch; A ch = ‘A’
cin >> ch; AB ch = ‘A’, ‘B’ is held for later input
cin >> a; 45 a = 45
cin >> a; 45.74 a= 45, .74 is held for later input
cin >> z; 45.74 z = 45.74
cin >> z; 45 z = 45.0
cin >> a >> z; 45.74 a= 45, z = .74
cin >> z >> a; 45.74 z= 45.74, computer waits for the input value for a
cin >> ch >> a; 234 ch = ‘2’, a = 34
cin and the extraction operator
int a;
Statement Input Values stored in memory
double z;
char ch, ch1, ch2; cin >> ch; A ch = ‘A’
cin >> ch; AB ch = ‘A’, ‘B’ is held for later input
cin >> a; 45 a = 45
cin >> a; 45.74 a= 45, .74 is held for later input
cin >> z; 45.74 z = 45.74
cin >> z; 45 z = 45.0
cin >> a >> z; 45.74 a= 45, z = .74
cin >> z >> a; 45.74 z= 45.74, computer waits for the input value for a
cin >> ch >> a; 234 ch = ‘2’, a = 34
cin >> a >> ch; 234 a = 234, computer waits for the input value for ch
cin and the extraction operator
int a;
Statement Input Values stored in memory
double z;
char ch, ch1, ch2; cin >> ch; A ch = ‘A’
cin >> ch; AB ch = ‘A’, ‘B’ is held for later input
cin >> a; 45 a = 45
cin >> a; 45.74 a= 45, .74 is held for later input
cin >> z; 45.74 z = 45.74
cin >> z; 45 z = 45.0
cin >> a >> z; 45.74 a= 45, z = .74
cin >> z >> a; 45.74 z= 45.74, computer waits for the input value for a
cin >> ch >> a; 234 ch = ‘2’, a = 34
cin >> a >> ch; 234 a = 234, computer waits for the input value for ch
cin >> ch1 >> ch2; AB ch1 = ‘A’, ch2 = ‘B’
cin and the extraction operator
int a;
Statement Input Values stored in memory
double z;
char ch, ch1, ch2; cin >> ch; A ch = ‘A’
cin >> ch; AB ch = ‘A’, ‘B’ is held for later input
cin >> a; 45 a = 45
cin >> a; 45.74 a= 45, .74 is held for later input
cin >> z; 45.74 z = 45.74
cin >> z; 45 z = 45.0
cin >> a >> z; 45.74 a= 45, z = .74
cin >> z >> a; 45.74 z= 45.74, computer waits for the input value for a
cin >> ch >> a; 234 ch = ‘2’, a = 34
cin >> a >> ch; 234 a = 234, computer waits for the input value for ch
cin >> ch1 >> ch2; AB ch1 = ‘A’, ch2 = ‘B’
cin >> a >> z >> ch; 2 3.4 A a = 2, z = 3.4, ch = ‘A’
cin and the extraction operator
int a;
Statement Input Values stored in memory
double z;
char ch, ch1, ch2; cin >> ch; A ch = ‘A’
cin >> ch; AB ch = ‘A’, ‘B’ is held for later input
cin >> a; 45 a = 45
cin >> a; 45.74 a= 45, .74 is held for later input
cin >> z; 45.74 z = 45.74
cin >> z; 45 z = 45.0
cin >> a >> z; 45.74 a= 45, z = .74
cin >> z >> a; 45.74 z= 45.74, computer waits for the input value for a
cin >> ch >> a; 234 ch = ‘2’, a = 34
cin >> a >> ch; 234 a = 234, computer waits for the input value for ch
cin >> ch1 >> ch2; AB ch1 = ‘A’, ch2 = ‘B’
cin >> a >> z >> ch; 2 3.4 A a = 2, z = 3.4, ch = ‘A’
cin >> a >> z >> ch; 23.4A a = 23, z = .4, ch = ‘A’
cin and the get function
• The extraction operator skips all leading whitespace characters (such as blanks or newline) when
scanning for the next input value.
• Example
char ch1, ch2;
int num;
and the input: A 25
Now, consider the following statement:
cin >> ch1 >> ch2 >> num;
After execution of the program, ‘A’ is stored in ch1, after skipping the blank space, ‘2’ is stored in
ch2 and 5 is stored in num. The extraction operator does not access the whitespace characters.
• The variable cin can access the stream function get, which is used to read character data.
• The get function inputs the very next character, including whitespace characters, from the input
stream and stores it in the memory location indicated by its argument.
cin and the get function
• Syntax of cin, together with get function
cin.get(VarChar);
where VarChar is a char variable and called the argument or parameter of the function.
• This statement store the next input character in the variable VarChar.
• Example
char ch1, ch2;
int num;
and the input: A 25
To store ‘A’ in ch1, the blank in ch2 and 25 in num, we will use
cin.get(ch1); or cin >> ch1;
cin.get(ch2);
cin >> num;
• The get function has only one argument and reads only one character. If you want to read more than
one character from the input stream, you need to call this function more than one time.
• The get function reads only the char types values.
ch1 ch2
stream buffer
a b c
stream buffer
b c
stream buffer
b c
a b
stream buffer
a b
stream buffer
cin.ignore(IntExp, ChExp);
where IntExp is an integer expression yielding an integer value and ChExp is a char expression
yielding a char value.
• The value of the expression IntExp specifies the maximum number of characters to be ignored
in a line.
• The above expression says to ignore the next IntExp characters or ignore the input until it
encounters the character specified by ChExp, whichever comes first.
cin and the ignore function
The putback functions
• The stream function putback puts the last character extracted from the input stream by the get
function back into the input stream i.e. decrease the current location in the stream by one
character.
• Syntax of putback function:
cin.putback(ch);
stream buffer
a b c
stream buffer
a b c
a a
stream buffer
b c
a a
stream buffer
b c
a a
stream buffer
b c
a a
stream buffer
d b c
a d
stream buffer
d b c
ch = istreamVar.peek( );
here istreamVar is the input stream variable (such as cin) and ch is a char variable.
The Dot Notation between I/O Stream variables
and I/O function: A precaution