Download as pdf or txt
Download as pdf or txt
You are on page 1of 44

Input stream functions

I/O STREAMS AND STANDARD I/O DEVICES


• A program performs three basic operations:
it gets data,
it manipulates the data
it outputs the results.
• In C++, I/O is a sequence of bytes, called a stream, from the source (keyboard) to the destination
(screen).
• There are two types of streams:
Stream

Input stream output stream

Input stream: A sequence of characters from an input device to the computer.


Output stream: A sequence of characters from the computer to an output device.
I/O STREAMS AND STANDARD I/O DEVICES

• The standard input device is usually the keyboard.


• The standard output device is usually the screen.
• Every C++ program must use the header file iostream to receive data from the keyboard and the
send output to the screen.
• iostream header file contains the definition of two data types, istream (input stream) and
ostream (output stream) and also contains two variables declarations, one for cin (stands for
common input) and other for cout (stands for common output).
• To use cin and cout, every C++ program must use the preprocessor directive: #include
<iostream>
• The variables of type istream and ostream are called input stream variables and output
stream variables, respectively.
• A stream variable is either an input stream variable or an output stream variable.
cin and the extraction operator
• The predefined object cin is an instance of istream class.
• Syntax:

cin >> variable >> variable >> variable… ;

• 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

char ch1, ch2;


cin.get(ch1);
cin.get(ch2);
ch1 ch2

stream buffer

b c

char ch1, ch2;


cin.get(ch1);
cin.get(ch2);
ch1 ch2

stream buffer

b c

char ch1, ch2;


cin.get(ch1);
cin.get(ch2);
ch1 ch2

a b

stream buffer

char ch1, ch2;


cin.get(ch1);
cin.get(ch2);
ch1 ch2

a b

stream buffer

char ch1, ch2;


cin.get(ch1);
cin.get(ch2);
cin and the get function
cin and the ignore function
• The stream function ignore is used to discard a portion of the input.
• Syntax of cin, together with ignore function

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);

here ch is a char variable.


• putback is opposite of get function.
ch1 ch2

stream buffer

a b c

char ch1, ch2;


ch1 = cin.peek();
cin.get(ch2);
cin.putback(ch2);
cin.get(ch2);
ch1 ch2

stream buffer

a b c

char ch1, ch2;


ch1 = cin.peek();
cin.get(ch2);
cin.putback(ch2);
cin.get(ch2);
ch1 ch2

a a

stream buffer

b c

char ch1, ch2;


ch1 = cin.peek();
cin.get(ch2);
cin.putback(ch2);
cin.get(ch2);
ch1 ch2

a a

stream buffer

b c

char ch1, ch2;


ch1 = cin.peek();
cin.get(ch2);
cin.putback(ch2);
cin.get(ch2);
ch1 ch2

a a

stream buffer

b c

char ch1, ch2;


ch1 = cin.peek();
cin.get(ch2);
cin.putback(ch2);
cin.get(ch2);
ch1 ch2

a a

stream buffer

d b c

char ch1, ch2;


ch1 = cin.peek();
cin.get(ch2);
cin.putback(‘d’);
cin.get(ch2);
ch1 ch2

a d

stream buffer

d b c

char ch1, ch2;


ch1 = cin.peek();
cin.get(ch2);
cin.putback(‘d’);
cin.get(ch2);
The peek functions
• The stream function peek looks into input stream and tells you what the next character is without
removing it from the input stream i.e. it checks the identity of the next input character.
• After checking the next input character in the input stream, it can store this character in a
designated memory location without removing it from the input stream i.e. when you use the peek
function, the next input character stays the same, even though you know what it is.
• Syntax of peek function:

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

• cin.get(ch); (cin and get identifiers separate by a dot)


• Omitting the dot- that is, the period between the variable cin ad the function
name get- results in a syntax error.
• cinget(ch); here cinget becomes a new identifier. If you used cinget(ch); in a
program, the compiler would try to resolve an undeclared identifier, which would
generate an error.
• The dot separates the input stream variable name from the member or the
function name.
• In C++, the dot is an operator and called the member access operator.
The putback and peek functions
Input/output and the string type
• the input stream variable such as cin and the extraction operator >> is used to
read a string into a variable of the data type string.
• The extraction operator skips any leading whitespace characters and that reading
stops at a whitespace character.
• The extraction operator can not use to read string that contain blanks.
• Example:
The string type and getline function
• The getline function is used to read a string containing blanks.
• The syntax:
getline(istreamVar, strVar);
where istreamVar is an input stream variable and strVar is a string variable.
• The function getline reads the string until it reaches the end of the current line.
Other points
• The functions get, ignore, putback and peek are associated with data type
istream and are called istream member function.
• I/O functions such as get, are typically called stream member functions or
stream functions.
• To include the mathematical functions in C++, the program must include line
#include<cmath> in the header.
• The function pow(x, y) is called the power function and used to calculate x^y in
the program. The numbers x and y are called the arguments or parameters.
• To determine the length of the string in C++, the program must include the line
#include<string> in the header.
• The function length is used to determine the length of the string.
Example

You might also like