www.gibmonks.com

Main Page




Previous Page
Next Page

[Page 804 (continued)]

Self-Review Exercises

15.1

Answer each of the following:

  1. Input/output in C++ occurs as ____________ of bytes.

  2. The stream manipulators that format justification are_________, _________ and _______.

  3. Member function _________ can be used to set and reset format state.

  4. Most C++ programs that do I/O should include the _________ header file that contains the declarations required for all stream-I/O operations.

  5. When using parameterized manipulators, the header file ____________ must be included.

  6. Header file __________ contains the declarations required for user-controlled file processing.

  7. The ostream member function ___________ is used to perform unformatted output.

  8. Input operations are supported by class __________.

  9. Outputs to the standard error stream are directed to either the ___________ or the ___________ stream object.

  10. Output operations are supported by class ___________.

  11. The symbol for the stream insertion operator is ____________.

  12. The four objects that correspond to the standard devices on the system include _________, _________, __________ and ___________.


  13. [Page 805]
  14. The symbol for the stream extraction operator is __________.

  15. The stream manipulators ___________, __________ and ___________ specify that integers should be displayed in octal, hexadecimal and decimal formats, respectively.

  16. When used, the _________ stream manipulator causes positive numbers to display with a plus sign.

15.2

State whether the following are true or false. If the answer is false, explain why.

  1. The stream member function flags with a long argument sets the flags state variable to its argument and returns its previous value.

  2. The stream insertion operator << and the stream-extraction operator >> are overloaded to handle all standard data typesincluding strings and memory addresses (stream-insertion only)and all user-defined data types.

  3. The stream member function flags with no arguments resets the stream's format state.

  4. The stream extraction operator >> can be overloaded with an operator function that takes an istream reference and a reference to a user-defined type as arguments and returns an istream reference.

  5. The stream insertion operator << can be overloaded with an operator function that takes an istream reference and a reference to a user-defined type as arguments and returns an istream reference.

  6. Input with the stream extraction operator >> always skips leading white-space characters in the input stream, by default.

  7. The stream member function rdstate returns the current state of the stream.

  8. The cout stream normally is connected to the display screen.

  9. The stream member function good returns TRue if the bad, fail and eof member functions all return false.

  10. The cin stream normally is connected to the display screen.

  11. If a nonrecoverable error occurs during a stream operation, the bad member function will return TRue.

  12. Output to cerr is unbuffered and output to clog is buffered.

  13. 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.

  14. The ostream member function put outputs the specified number of characters.

  15. The stream manipulators dec, oct and hex affect only the next integer output operation.

  16. By default, memory addresses are displayed as long integers.

15.3

For each of the following, write a single statement that performs the indicated task.

  1. Output the string "Enter your name: ".

  2. Use a stream manipulator that causes the exponent in scientific notation and the letters in hexadecimal values to print in capital letters.

  3. Output the address of the variable myString of type char *.

  4. Use a stream manipulator to ensure floating-point values print in scientific notation.

  5. Output the address in variable integerPtr of type int *.

  6. Use a stream manipulator such that, when integer values are output, the integer base for octal and hexadecimal values is displayed.

  7. Output the value pointed to by floatPtr of type float *.

  8. Use a stream member function to set the fill character to '*' for printing in field widths larger than the values being output. Write a separate statement to do this with a stream manipulator.

  9. Output the characters '0' and 'K' in one statement with ostream function put.

  10. Get the value of the next character in the input stream without extracting it from the stream.


  11. [Page 806]
  12. Input a single character into variable charValue of type char, using the istream member function get in two different ways.

  13. Input and discard the next six characters in the input stream.

  14. Use istream member function read to input 50 characters into char array line.

  15. 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.

  16. 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.

  17. Output the following values: 124, 18.376, 'Z', 1000000 and "String".

  18. Print the current precision setting, using a member function of object cout.

  19. Input an integer value into int variable months and a floating-point value into float variable percentageRate.

  20. Print 1.92, 1.925 and 1.9258 separated by tabs and with 3 digits of precision, using a manipulator.

  21. Print integer 100 in octal, hexadecimal and decimal, using stream manipulators.

  22. Print integer 100 in decimal, octal and hexadecimal, using a stream manipulator to change the base.

  23. Print 1234 right justified in a 10-digit field.

  24. 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.

  25. 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.

15.4

Identify the error in each of the following statements and explain how to correct it.

  1. cout << "Value of x <= y is: " << x <= y;
    
  2. The following statement should print the integer value of 'c'.

    cout << 'c';
    
  3. cout << ""A string in quotes"";
    
15.5

For each of the following, show the output.

  1. cout << "12345" << endl;
    cout.width( 5 );
    cout.fill( '*' );
    cout << 123 << endl << 123;
    
  2. cout << setw( 10 ) << setfill( '$' ) << 10000;
    
  3. cout << setw( 8 ) << setprecision( 3 ) << 1024.987654;
    
  4. cout << showbase << oct << 99 << endl << hex << 99;
    
  5. cout << 100000 << endl << showpos << 100000;
    
  6. cout << setw( 10 ) << setprecision( 2 ) << scientific << 444.93738;
    


Previous Page
Next Page