Self-Review Exercises

  1. 13.1 (Fill in the Blanks) Answer each of the following:

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

    2. The stream manipulators for 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 that contains the declarations required for all stream-I/O operations.

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

    6. The               stream manipulator causes positive numbers to display with a plus sign.

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

    8. Input operations are supported by class              .

    9. Standard error stream outputs are directed to the stream objects               or              .

    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. The symbol for the stream extraction operator is              .

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

  2. 13.2 (True or False) State whether each of the following is 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 types—including 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. Input with the stream extraction operator >> always skips leading white-space characters in the input stream, by default.

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

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

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

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

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

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

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

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

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

  3. 13.3 (Write a C++ Statement) 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 that 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. Repeat this statement with a stream manipulator.

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

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

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

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

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

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

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

    16. Output 124, 18.376, 'Z', 1000000 and "String", separated by spaces.

    17. Display cout’s current precision setting.

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

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

    20. Print integer 100 in octal, hexadecimal and decimal, using stream manipulators and separated by tabs.

    21. Print integer 100 in decimal, octal and hexadecimal separated by tabs, using a stream manipulator to change the base.

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

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

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

  4. 13.4 (Find and Correct Code Errors) 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"";

  5. 13.5 (Show Outputs) For each of the following, show the output.

    1.  

      
      cout << "12345
      ";
      cout.width(5);
      cout.fill('*');
      cout << 123 << "
      " << 123;
      
    2. cout << setw(10) << setfill('$') << 10000;

    3. cout << setw(8) << setprecision(3) << 1024.987654;

    4. cout << showbase << oct << 99 << " " << hex << 99;

    5. cout << 100000 << " " << showpos << 100000;

    6. cout << setw(10) << setprecision(2) << scientific << 444.93738;

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset