Lab 3 1 Examining Predefined Input Functions
Lab 3 1 Examining Predefined Input Functions
In C++, I/O is accomplished through a stream, which is a sequence of bytes (characters) from
the source to the destination. The exception occurs when using images and sound. To
receive data from the keyboard and send output to the screen, every C++ program must use
the header file iostream, which includes a set of functions to perform specific I/O tasks.
To use the variables cin and cout, use the preprocessor directive:
#include <iostream>
You use the variable cin with the extraction operator >> to input data from the keyboard.
Additionally, C++ allows the cin statement to read more than one data item by using the
extraction operator several times. The extraction operator >> is binary and thus takes two
operands. The left operand must be an input stream variable such as cin. The right
operand is a variable. The extraction operator >> skips all whitespace characters. When
reading a char variable, after the program ignores whitespace, it reads only the next
character. When reading an int or a float, a program reads digits (including possible
leading + or - signs) until it finds whitespace or a character. The int type also stops at a
decimal point. If no decimal is entered for a float type, the value is converted to a float
type when the receiving variable is of the float type.
The main function executes automatically when you run a program. Other functions
execute only when they are activated or called. C++ comes with predefined functions and
variables that are already included in header files. The iostream header file contains a
variable declaration for cin. A useful predefined function found in the cmath header file is
the pow function, which is used to calculate a base number raised to an exponent. To use a
predefined function in a program, you need to know the name of the header file containing
the specifications of the function and include that header file in the program. In addition,
you need to know the name of the function, the number of parameters the function takes,
and the type of each parameter. You must also be aware of what the function is going to do.
The variable cin has access to operators and functions that can extract data from the
standard input device. You can use dot notation to associate the input stream variable name
with the function name. 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 ignore function allows the program to discard a portion of the input.
The maximum number of characters to be ignored can be stipulated or input is ignored until
a specified character is encountered, whichever comes first. Additionally, the putback
function puts the last character extracted from the input stream by the get function back
into the input stream. The peek function returns the next character from the input stream
but does not remove the character from that stream.
The variables cin and cout are data types of the istream and ostream classes found
in the iostream header file. These variables also have special names, called objects.
After the program terminates, any values left in the input stream are discarded. Other than
a float type accepting integer input, no other mismatch of data is allowed. When a C++
program attempts to read invalid data, input failure occurs, causing the input stream to
enter the fail state. Once an input stream enters a fail state, all further I/O statements using
that stream are ignored. The stream function clear can restore the input stream to a
working state.
Objectives
In this lab, you become acquainted with using I/O streams and the predefined functions
get, ignore, putback, and peek.
After completing this lab, you will be able to:
• Use the I/O predefined functions get, ignore, putback, and peek.
1. When using cin and the extraction operator for multiple True False
input items, the items are separated by commas.
2. When using cin and the extraction operator to input numeric True False
data, the + operator is optional.
3. The statements: True False
cin >> x;
cin >> y;
require that items are input on different lines, whereas the
statement:
cin >> x >> y
requires that items are input on the same line.
4. If the user is prompted to enter an integer value, press the True False
Enter key, and then enter another integer value, the cin
statement to read those values is as follows:
cin >> x >> endl >> y;
5. When using cin and the extraction operator, the newline True False
character is treated the same as other whitespace characters.
6. When using the get function, whitespace characters are True False
treated the same as other characters.
7. The peek function does not remove characters from the True False
input stream.
8. To enter a newline character as part of a stream, you press True False
the Enter key.
9. When input failure occurs, the program terminates. True False
10. The stream function putback lets you put the last character True False
extracted from the input stream by the get function back
into the input stream.
11a. Design a program that prompts the user to enter a first name, middle name or initial,
and last name, with each name separated by a space. Display only the user’s initials
on the screen. (Use a combination of the get and ignore functions.)
Write your design in the following space. Your design should be a list of C++
comments without any code.
11b. Write a C++ program based on the design you created in Exercise 11a and name it
initial1.cpp. Step through the code by hand.
Use the following memory table to show what occurs in memory when the C++ code
is executed. (Include line numbers as documentation only. Do not use line numbers
when entering your final program.) To fill out the memory table, use one or two lines
for each variable. On one line, enter declaration information. Write the name of the
declared variable, its data type, and the line number at declaration.
In the following space, show what is displayed on the screen after executing the output
message.
11c. Enter, compile, link, and execute initial1.cpp. Then copy the output and save it in a
block comment at the end of your program. Save initial1.cpp in the Chap03 folder of
your Student Data Files.
The following is a copy of the screen results that might appear after running your
program, depending on the data entered. The input entered by the user is shown in
bold.
11d. What would occur if the user begins the input with one or more spaces?
11e. What would occur if the names were separated by more than one space?
12a. Design a program that prompts the user to enter a first name, middle name or initial,
and last name, with each name separated by a space. Display the user’s first name,
middle name or initial, and last name on the screen. On another line, display only the
user’s initials. (Use a combination of the get and putback functions.)
Write your design in the following space. Your design should be a list of C++
comments without any code.
12b. Write a C++ program based on the design you created in Exercise 12a and name it
initial2.cpp. Step through the code by hand.
Use the following memory table to show what occurs in memory when the C++ code
is executed. (Include line numbers as documentation only. Do not use line numbers
when entering your final program.) To fill out the memory table, use one or two lines
for each variable. On one line, enter declaration information. Write the name of the
declared variable, its data type, and the line number at declaration.
In the following space, show what is displayed on the screen after executing the
output message.
12c. Enter, compile, link, and execute initial2.cpp. Then copy the output and save it in a
block comment at the end of your program. Save initial2.cpp in the Chap03 folder of
your Student Data Files.
The following is a copy of the screen results that might appear after running your
program, depending on the data entered. The input entered by the user is shown in
bold.
Enter your first name, middle name or initial, and last name separated by spaces:
Jane Ann Doe
Your name is: Jane Ann Doe
Your initials are: JAD
12d. What would occur if the user begins the input with one or more spaces?
12e. What would occur if the names were separated by more than one space?
13a. Design a program that prompts the user to enter a first name, middle name or initial,
and last name, with each name separated by a space. Display the user’s first name,
middle name or initial, and last name on the screen. On another line, display only the
user’s initials. (Use a combination of the get and peek functions.)
Write your design in the following space. Your design should be a list of C++
comments without any code.
13b. Write a C++ program based on the design you created in Exercise 13a and name it
initial3.cpp. Step through the code by hand.
Use the following memory table to show what occurs in memory when the C++ code
is executed. (Include line numbers as documentation only. Do not use line numbers
when entering your final program.) To fill out the memory table, use one or two lines
for each variable. On one line, enter declaration information. Write the name of the
declared variable, its data type, and the line number at declaration.
In the following space, show what is displayed on the screen after executing the
output message.
13c. Enter, compile, link, and execute initial3.cpp. Then copy the output and save it in a
block comment at the end of your program. Save initial3.cpp in the Chap03 folder of
your Student Data Files.
The following is a copy of the screen results that might appear after running your
program, depending on the data entered. The input entered by the user is shown in
bold.
Enter your first name, middle name or initial, and last name separated by spaces:
Jane Ann Doe
Your name is: Jane Ann Doe
Your initials are: JAD
13d. What would occur if the user begins the input with one or more spaces?
13e. What would occur if the names were separated by more than one space?