Chapter 03
Chapter 03
Chapter 3: Input/Output
Objectives
input
3 cin >> a; 48 a = 48
19 cin >> a >> ch; 256 a = 256, computer waits for the
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
cin and the get Function
• 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.
• The get function
− Inputs next character (including whitespace)
− Stores character location indicated by its argument
• The syntax of cin and the get function:
cin.get(varChar);
varChar
− Is a char variable
− Is the argument (parameter) of the function
• 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
• In the statement
cin.get(ch);
• Expression is evaluated
• Value is printed
• Manipulator is used to format the output
string movieName;
double adultTicketPrice;
double childTicketPrice;
int noOfAdultTicketsSold;
int noOfChildTicketsSold;
double percentDonation;
double grossAmount;
double amountDonated;
double netSaleAmount;
C++ Programming: From Problem Analysis to Program Design, Third Edition 46
Formatting Output
• First column is left-justified
• Numbers in second column are right-justified
• When printing a value in the first column, use
left
• 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