III Input output
III Input output
Chapter 3: Input/Output
Objectives
2
Objectives (continued)
3
I/O Streams and Standard I/O
Devices
• I/O: sequence of bytes (stream of bytes) from
source to destination
− Bytes are usually characters, unless program
requires other types of information
• Stream: sequence of characters from source
to destination
• Input stream: sequence of characters from an
input device to the computer
• Output stream: sequence of characters from
the computer to an output device
4
I/O Streams and Standard I/O
Devices (continued)
• Use iostream header file to extract (receive)
data from keyboard and send output to the
screen
− Contains definitions of two data types:
• istream - input stream
• ostream - output stream
− Has two variables:
• cin - stands for common input
• cout - stands for common output
5
I/O Streams and Standard I/O
Devices (continued)
• To use cin and cout, the preprocessor
directive #include <iostream> must be
used
• Variable declaration is similar to:
− istream cin;
− ostream cout;
• Input stream variables: type istream
• Output stream variables: type ostream
6
cin and the Extraction Operator
>>
• The syntax of an input statement using cin
and the extraction operator >> is:
7
cin and the Extraction Operator
>> (continued)
• No difference between a single cin with
multiple variables and multiple cin
statements with one variable
• When scanning, >> skips all whitespace
− Blanks and certain nonprintable characters
• >> distinguishes between character 2 and
number 2 by the right-side operand of >>
− If type char or int (or double), the 2 is
treated as a character or as a number 2
8
cin and the Extraction Operator
>> (continued)
9
cin and the Extraction Operator
>> (continued)
• When reading data into a char variable
− >> skips leading whitespace, finds and stores
only the next character
− Reading stops after a single character
• To read data into an int or double variable
− >> skips leading whitespace, reads + or - sign
(if any), reads the digits (including decimal)
− Reading stops on whitespace non-digit
character
10
cin and the Extraction Operator
>> (continued)
11
Using Predefined Functions in a
Program
• Function (subprogram): set of instructions
− When activated, it accomplishes a task
• main executes when a program is run
• Other functions execute only when called
• C++ includes a wealth of functions
− Predefined functions are organized as a
collection of libraries called header files
13
Using Predefined Functions in a
Program (continued)
• Header file may contain several functions
• To use a predefined function, you need the
name of the appropriate header file
− You also need to know:
• Function name
• Number of parameters required
• Type of each parameter
• What the function is going to do
14
Using Predefined Functions in a
Program (continued)
• To use pow (power), include cmath
− Two numeric parameters
− Syntax: pow(x,y) = xy
• x and y are the arguments or parameters
15
Using Predefined Functions in a
Program (continued)
Sample Run:
Line 1: 2 to the power of 6 = 64
Line 4: 12.5 to the power of 3 = 1953.13
Line 5: Square root of 24 = 4.89898
Line 7: u = 181.019
Line 9: Length of str = 20
17
cin and the get Function
varChar
− Is a char variable
− Is the argument (parameter) of the function
18
cin and the ignore Function
19
putback and peek Functions
• putback function
− Places previous character extracted by the
get function from an input stream back to that
stream
• peek function
− Returns next character from the input stream
− Does not remove the character from that
stream
20
putback and peek Functions
(continued)
• The syntax for putback:
21
The Dot Notation Between I/O
Stream Variables and I/O Functions
• In the statement
cin.get(ch);
cin and get are two separate identifiers
separated by a dot
• Dot separates the input stream variable name
from the member, or function, name
• In C++, dot is the member access operator
22
Input Failure
23
The clear Function
24
Output and Formatting Output
• Expression is evaluated
• Value is printed
• Manipulator is used to format the output
− Example: endl
25
setprecision Manipulator
• Syntax:
26
fixed Manipulator
27
showpoint Manipulator
28
setw
30
setfill Manipulator
• Example:
− cout << setfill('#');
31
left and right Manipulators
32
Types of Manipulators
33
Input/Output and the string
Type
• An input stream variable (cin) and >>
operator can read a string into a variable of
the data type string
• Extraction operator
− Skips any leading whitespace characters and
reading stops at a whitespace character
• The function getline
− Reads until end of the current line
34
File Input/Output
35
Programming Example: Movie
Ticket Sale and Donation to Charity
• A theater owner agrees to donate a portion of
gross ticket sales to a charity
• The program will prompt the user to input:
− Movie name
− Adult ticket price
− Child ticket price
− Number of adult tickets sold
− Number of child tickets sold
− Percentage of gross amount to be donated
36
Programming Example: I/O
37
Programming Example: Problem
Analysis
• The program needs to:
1. Get the movie name
2. Get the price of an adult ticket price
3. Get the price of a child ticket price
4. Get the number of adult tickets sold
5. Get the number of child tickets sold
38
Programming Example: Problem
Analysis (continued)
6. Calculate the gross amount
grossAmount = adultTicketPrice *
noOfAdultTicketsSold + childTicketPrice *
noOfChildTicketsSold;
39
Programming Example: Variables
string movieName;
double adultTicketPrice;
double childTicketPrice;
int noOfAdultTicketsSold;
int noOfChildTicketsSold;
double percentDonation;
double grossAmount;
double amountDonated;
double netSaleAmount;
40
Programming Example:
Formatting Output
• First column is left-justified
− When printing a value in the first column, use
left
• Numbers in second column are right-justified
− Before printing a value in the second column,
use right
• Use setfill to fill the empty space between
the first and second columns with dots
41
Programming Example:
Formatting Output (continued)
• In the lines showing gross amount, amount
donated, and net sale amount
− Use blanks to fill space between the $ sign
and the number
• Before printing the dollar sign
− Use setfill to set the filling character to
blank
42
Programming Example: Main
Algorithm
1. Declare variables
2. Set the output of the floating-point to:
− Two decimal places
− Fixed
− Decimal point and trailing zeros
3. Prompt the user to enter a movie name
4. Input movie name using getline because
it might contain spaces
5. Prompt user for price of an adult ticket
43
Programming Example: Main
Algorithm (continued)
6. Input price of an adult ticket
7. Prompt user for price of a child ticket
8. Input price of a child ticket
9. Prompt user for the number of adult tickets
sold
10. Input number of adult tickets sold
11. Prompt user for number of child tickets sold
12. Input the number of child tickets sold
44
Programming Example: Main
Algorithm (continued)
13. Prompt user for percentage of the gross
amount donated
14. Input percentage of the gross amount
donated
15. Calculate the gross amount
16. Calculate the amount donated
17. Calculate the net sale amount
18. Output the results
45
Summary
47
Summary (continued)
48