Programming Fundamental 5 Lec
Programming Fundamental 5 Lec
Fundamental
s
Instructor: Noor Ullah Khan
Input/
Output
A program performs three basic
operations: It gets data, it manipulates
I/O Streams the data, and it outputs the results.
and
In last chapter you learned how to
Standard manipulate numeric data using
I/O Devices arthimetic operations.
The extraction operator >> is binary and thus takes two operands.
A single input statement can read more than one data item by using the operator
>> several times.
Every occurrence of >> extracts the next data item from the input stream.
For example, you can read both payrate cin >> payRate >> hoursWorked;
and hoursWorked via a single input cin >> payRate;
statement by using the following code: cin >> hoursWorked;
cin and Extraction Operator (>>)
When scanning for the next input, >> skips all whitespace characters.
Thus, whether you separate the input data by lines or blanks, the extraction
operator >> simply finds the next input data in the input stream.
cin and
Extraction
Operator
(>>)
cin and Extraction Operator (>>)
cin and Extraction
Operator (>>)
cin and
Extraction
Operator
(>>)
cin and
Extraction
Operator
(>>)
cin and
Extraction
Operator
(>>)
• Recall that during program
execution, when entering
character data such as letters,
you do not enter the single
cin and quotes around the character.
Extraction
Operator
(>>)
cin and Extraction Operator (>>)
The next few sections teach you how to input data into a program
using the input functions, such as get, ignore, putback, and peek.
cin and the get Function
As you have seen, the
extraction operator skips all
Consider the variable Now consider the following
leading whitespace and the input:
declarations: statement:
characters when scanning
for the next input value.
int num;
cin and the get Function
For example, suppose you want to process the entered data on a line-
by-line basis.
cin and the get Function
The effect of the preceding statement would be to store the next input character
in the variable varChar.
cin and the get Function
• Because this form of the get function has
only one argument and reads only one
character and you need to read two
cin and characters from the input stream, you need
to call this function twice.
the get • Notice that you cannot use the get function
Function to read data into the variable num because
num is an int variable.
• The preceding form of the get function
reads values of only the char data type.
• When you want to process only partial data
(say, within a line), you can use the stream
function ignore to discard a portion of the
cin and input.
the ignore • The syntax to use the function ignore is:
• cin.ignore(intExp, chExp);
Function • Here, intExp is an integer expression yielding
an integer value, and chExp is a char
expression yielding a char value.
• In fact, the value of the expression intExp
specifies the maximum number of characters
to be ignored in a line.
• Suppose intExp yields a value of, say 100.
cin and This statement says to ignore the next 100
characters or ignore the input until it
the ignore encounters the character specified by
Function chExp, whichever comes first.
• To be specific, consider the following
statement:
• cin.ignore(100, '\n');
• When this statement executes, it ignores
either the next 100 characters or all
cin and characters until the newline character is
found, whichever comes first.
the ignore • For example, if the next 120 characters do
Function not contain the newline character, then only
the first 100 characters are discarded and
the next input data is the character 101.
• However, if the 75th character is the newline
cin and character, then the first 75 characters are discarded
and the next input data is the 76th character.
For example, what would happen if you tried to input a letter into an int
variable?
If the input data did not match the corresponding variables, the program would
run into problems.
For example, trying to read a letter into an int or double variable would result
in an input failure.
• Consider the following statements:
• int a, b, c;
Input
• double x;
• If the input is:
• W 54
Failure
would result in
storing 35 in a,
67.93 in x, and
48 in b.
• Now consider the following read statement
with the previous input (the input with three
values):
• cin >> a >> b >> c;
Input • This statement stores 35 in a and 67 in b. The
reading stops at . (the decimal point).
Failure • Because the next variable c is of the data type
int, the computer tries to read . into c, which is
an error.
• The input stream then enters a state called
the fail state.
• What actually happens when the input stream
enters the fail state?
Failure
ignored.
• Unfortunately, the program quietly continues
to execute with whatever values are stored in
variables and produces incorrect results.
Input Failure
The clear Function
The Clear Function
• When an input stream enters the fail state, the system ignores all further I/O using
• that stream. You can use the stream function clear to restore the input stream to a
• working state.
• The syntax to use the function clear is:
• istreamVar.clear();
• Here, istreamVar is an input stream variable, such as cin.
Clear and Ignore function