www.gibmonks.com

Main Page




Previous Page
Next Page

[Page 806 (continued)]

Answers to Self-Review Exercises

15.1

a) streams. b) left, right and internal. c) flags. d) <iostream>. e) <iomanip>. f) <fstream>. g) write. h) istream. i) cerr or clog. j) ostream. k) <<. l) cin, cout, cerr and clog. m) >>. n) oct, hex and dec. o) showpos.

15.2

a) False. The stream member function flags with a fmtflags argument sets the flags state variable to its argument and returns the prior state settings. b) False. The stream insertion and stream extraction operators are not overloaded for all user-defined types. The programmer of a class must specifically provide the overloaded operator functions to overload the stream operators for use with each user-defined type. c) False. The stream member function flags with no arguments returns the current format settings as a fmtflags data type, which represents the format state. d) True. e) False. To overload the stream insertion operator <<, the overloaded operator function must take an ostream reference and a reference to a user-defined type as arguments and return an ostream reference. f) True. g) True. h) True. i) True. j) False. The cin stream is connected to the standard input of the computer, which normally is the keyboard. k) True. l) True. m) True. n) False. The ostream member function put outputs its single-character argument. o) False. The stream manipulators dec, oct and hex set the output format state for integers to the specified base until the base is changed again or the program terminates. p) False. Memory addresses are displayed in hexadecimal format by default. To display addresses as long integers, the address must be cast to a long value.


[Page 807]
15.3
  1. cout << "Enter your name: ";
    
  2. cout << uppercase;
    
  3. cout << static_cast< void *  >( myString );
    
  4. cout << scientific;
    
  5. cout << integerPtr;
    
  6. cout << showbase;
    
  7. cout << *floatPtr;
    
  8. cout.fill( '*' );
    cout << setfill( '*' );
    
  9. cout.put( '0' ).put( 'K' );
    
  10. cin.peek();
    
  11. charValue = cin.get();
    cin.get( charValue );
    
  12. cin.ignore( 6 );
    
  13. cin.read( line, 50 );
    
  14. cin.get( name, 10, '.' );
    cin.getline( name, 10, '.' );
    
  15. cout.write( line, cin.gcount() );
    
  16. cout << 124 << ' ' << 18.376 << ' ' << "Z" << 1000000 << "String";
    
  17. cout << cout.precision();
    
  18. cin >> months >> percentageRate;
    
  19. cout << setprecision( 3 ) << 1.92 << '\t' << 1.925 << '\t' << 1.9258;
    
  20. cout << oct << 100 << '\t' << hex << 100 << '\t' << dec << 100;
    
  21. cout << 100 << '\t' << setbase( 8 ) << 100 << '\t' << setbase( 16 ) << 100;
    
  22. cout << setw( 10 ) << 1234;
    
  23. cin.get( line, 20, 'z' );
    
  24. cout << setw( x ) << setprecision( y ) << 87.4573;
    
15.4
  1. Error: The precedence of the << operator is higher than that of <=, which causes the statement to be evaluated improperly and also causes a compiler error.

    Correction: To correct the statement, place parentheses around the expression x <= y. This problem will occur with any expression that uses operators of lower precedence than the << operator if the expression is not placed in parentheses.

  2. Error: In C++, characters are not treated as small integers, as they are in C.

    Correction: To print the numerical value for a character in the computer's character set, the character must be cast to an integer value, as in the following:

    cout << static_cast< int >( 'c' );
    
  3. Error: Quote characters cannot be printed in a string unless an escape sequence is used.

    Correction: Print the string in one of the following ways:

    cout << '"' << "A string in quotes" << '"';
    cout << "\"A string in quotes\"";
    

[Page 808]
15.5
  1. 12345
    **123
    123
    
  2. $$$$$10000
    
  3. 1024.988
    
  4. 0143
    0x63
    
  5. 100000
    +100000
    
  6. 4.45e+002
    


Previous Page
Next Page