13.1 (Fill in the Blanks) Answer each of the following:
Input/output in C++ occurs as of bytes.
The stream manipulators for justification are , and .
Member function can be used to set and reset format state.
Most C++ programs that do I/O should include the header that contains the declarations required for all stream-I/O operations.
When using parameterized manipulators, the header must be included.
The stream manipulator causes positive numbers to display with a plus sign.
The ostream
member function is used to perform unformatted output.
Input operations are supported by class .
Standard error stream outputs are directed to the stream objects or .
Output operations are supported by class .
The symbol for the stream insertion operator is .
The four objects that correspond to the standard devices on the system include , , and .
The symbol for the stream extraction operator is .
The stream manipulators , and specify that integers should be displayed in octal, hexadecimal and decimal formats, respectively.
13.2 (True or False) State whether each of the following is true or false. If the answer is false, explain why.
The stream member function flags
with a long
argument sets the flags
state variable to its argument and returns its previous value.
The stream insertion operator <<
and the stream extraction operator >>
are overloaded to handle all standard data types—including strings and memory addresses (stream insertion only)—and all user-defined data types.
The stream member function flags
with no arguments resets the stream’s format state.
Input with the stream extraction operator >>
always skips leading white-space characters in the input stream, by default.
The stream member function rdstate
returns the current state of the stream.
The cout
stream normally is connected to the display screen.
The stream member function good
returns true
if the bad
, fail
and eof
member functions all return false
.
The cin
stream normally is connected to the display screen.
If a nonrecoverable error occurs during a stream operation, the bad
member function will return true
.
Output to cerr
is unbuffered and output to clog
is buffered.
Stream manipulator showpoint
forces floating-point values to print with the default six digits of precision unless the precision value has been changed, in which case floating-point values print with the specified precision.
The ostream
member function put
outputs the specified number of characters.
The stream manipulators dec
, oct
and hex
affect only the next integer output operation.
13.3 (Write a C++ Statement) For each of the following, write a single statement that performs the indicated task.
Output the string "Enter your name: "
.
Use a stream manipulator that causes the exponent in scientific notation and the letters in hexadecimal values to print in capital letters.
Output the address of the variable myString
of type char *
.
Use a stream manipulator to ensure that floating-point values print in scientific notation.
Output the address in variable integerPtr
of type int *
.
Use a stream manipulator such that, when integer values are output, the integer base for octal and hexadecimal values is displayed.
Output the value pointed to by floatPtr
of type float *
.
Use a stream member function to set the fill character to '*'
for printing in field widths larger than the values being output. Repeat this statement with a stream manipulator.
Output the characters 'O'
and 'K'
in one statement with ostream
function put
.
Get the value of the next character to input without extracting it from the stream.
Input a single character into variable charValue
of type char
, using the istream
member function get
in two different ways.
Input and discard the next six characters in the input stream.
Use istream
member function read
to input 50 characters into char
array line
.
Read 10 characters into character array name
. Stop reading characters if the '.'
delimiter is encountered. Do not remove the delimiter from the input stream. Write another statement that performs this task and removes the delimiter from the input.
Use the istream
member function gcount
to determine the number of characters input into character array line
by the last call to istream
member function read
, and output that number of characters, using ostream
member function write
.
Output 124
, 18.376
, 'Z'
, 1000000
and "String
"
, separated by spaces.
Display cout
’s current precision setting.
Input an integer value into int
variable months
and a floating-point value into float
variable percentageRate
.
Print 1.92
, 1.925
and 1.9258
separated by tabs and with 3
digits of precision, using a stream manipulator.
Print integer 100
in octal, hexadecimal and decimal, using stream manipulators and separated by tabs.
Print integer 100
in decimal, octal and hexadecimal separated by tabs, using a stream manipulator to change the base.
Print 1234
right justified in a 10
-digit field.
Read characters into character array line
until the character 'z'
is encountered, up to a limit of 20
characters (including a terminating null character). Do not extract the delimiter character from the stream.
Use integer variables x
and y
to specify the field width and precision used to display the double
value 87.4573
, and display the value.
13.4 (Find and Correct Code Errors) Identify the error in each of the following statements and explain how to correct it.
cout << "Value of x <= y is: " << x <= y;
The following statement should print the integer value of '
c'
.
cout << 'c'
;
cout << ""A string in quotes"";
13.5 (Show Outputs) For each of the following, show the output.
cout << "12345
";
cout.width(5);
cout.fill('*');
cout << 123 << "
" << 123;
cout << setw(10) << setfill('$') << 10000;
cout << setw(8) << setprecision(3) << 1024.987654;
cout << showbase << oct << 99 << "
" << hex << 99;
cout << 100000 << "
" << showpos << 100000;
cout << setw(10) << setprecision(2) << scientific << 444.93738;