0% found this document useful (0 votes)
2 views

Programming Fundamental 5 Lec

This document provides an overview of input and output operations in C++, focusing on I/O streams, the extraction operator (>>), and various functions like get, ignore, putback, and peek. It explains how to read user input, handle whitespace, and manage input failures when data types do not match. Additionally, it covers the use of the clear function to restore the input stream after an error occurs.

Uploaded by

cyberexpert997
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Programming Fundamental 5 Lec

This document provides an overview of input and output operations in C++, focusing on I/O streams, the extraction operator (>>), and various functions like get, ignore, putback, and peek. It explains how to read user input, handle whitespace, and manage input failures when data types do not match. Additionally, it covers the use of the clear function to restore the input stream after an error occurs.

Uploaded by

cyberexpert997
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 47

Programming

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.

Now we will learn in detail how to


input from user and output data to
user.
In C++, I/O is a sequence of bytes,
called a stream, from the source to
I/O Streams the destination.
and
Standard The bytes are usually characters.
I/O Devices

Therefore, a stream is a sequence of


characters from the source to the
destination
Input stream: A sequence of characters
from an input device to the computer.
I/O Streams
and Output stream: A sequence of characters
from the computer to an output device
Standard
I/O Devices To use cin and cout you must have header
file <iostream>

It contains definition for both cin and


cout.
• Because cin and cout are already
defined in <iostream> header
I/O file, you should never redefine
them.
Streams
and
Standard
I/O
Devices
cin and Extraction Operator (>>)

cin >> payRate;


• When the computer executes this statement, it inputs the next number typed on the
keyboard and stores this number in payRate.
• Therefore, if the user types 15.50, the value stored in payRate is 15.50.

The extraction operator >> is binary and thus takes two operands.

The left-side operand must be an input stream variable, such as cin.


cin and Extraction Operator
(>>)
cin and Extraction Operator (>>)

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.

Recall that whitespace characters consist of blanks and certain nonprintable


characters, such as tabs and the newline character.

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 computer, however, does not tolerate any other kind of


mismatch.

For example, entering a char value into an int or double variable


causes serious errors, called input failure.
cin and Extraction Operator
(>>)
The extraction operator, when scanning for the next input in the input
stream, skips whitespace such as blanks and the newline character.

However, there are situations when these characters must also be


stored and processed.

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.

char ch1, cin >> ch1 >>


A 25
ch2; ch2 >> num;

int num;
cin and the get Function

As stated earlier, sometimes you need to process the entire input,


including whitespace characters, such as blanks and the newline
character.

For example, suppose you want to process the entered data on a line-
by-line basis.
cin and the get Function

Because the extraction operator >> skips the


newline character and unless the program
captures the newline character,

the computer does not know where one line ends


and the next begins.
cin and the • The variable cin can access the stream function get, which
get is used to read character data.
• The get function inputs the very next character, including
Function whitespace characters, from the input stream and stores it
in the memory location indicated by its argument.
cin and the get Function

The syntax of cin, together with the get


cin.get(varChar);
function to read a character, follows:

In the cin.get statement, varChar is a char variable. varChar, which appears in


parentheses following the function name, is called the argument or parameter of
the 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.

the • Similarly, the execution of the statement:


• cin.ignore(100, 'A');
ignore • results in ignoring the first 100 characters or all
characters until the character 'A’ is found,
Function whichever comes first.
cin and
the
ignore
Function
cin and
the
ignore
Function
cin and
the
ignore
Function
The stream function putback lets
The you put the last character extracted
from the input stream by the get
putback function back into the input stream.
and peek
Functions The stream function peek looks into
the input stream and tells you what
the next character is without
removing it from the input stream.
The
putback
and peek
Functions
Input Failure

What about an attempt to read invalid data?

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 • then the statement:


• cin >> a >> b;
• would result in an input failure, because
you are trying to input the character 'W'
into the int variable a.
If the input
35 67.93 48
were:

Input then the input


statement:
cin >> a >> x >> b;

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?

Input • Once an input stream enters the fail state, all


further I/O statements using that stream are

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

After using the function clear to return the input


stream to a working state, you still need to clear
the rest of the garbage from the input stream.

This can be accomplished by using the function


ignore.

You might also like