Wrap-Up

Answers to Self Review Exercises

15.1

a) streams. b) left, right and internal. c) flags. d) . e) . f) . 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.


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 << ' ' << 1.925 << ' ' << 1.9258;
  20. cout << oct << 100 << ' ' << hex << 100 << ' ' << dec << 100;
  21. cout << 100 << ' ' << setbase( 8 ) << 100 << ' ' << 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"";
    
 
15.5
  1. 12345 **123 123
  2. $$$$$10000
  3. 1024.988
  4. 0143 0x63
  5. 100000 +100000
  6. 4.45e+002

Exercises

15.6

Write a statement for each of the following:

  1. Print integer 40000 left justified in a 15-digit field.
  2. Read a string into character array variable state.
  3. Print 200 with and without a sign.
  4. Print the decimal value 100 in hexadecimal form preceded by 0x.
  5. Read characters into array charArray until the character 'p' is encountered, up to a limit of 10 characters (including the terminating null character). Extract the delimiter from the input stream, and discard it.
  6. Print 1.234 in a 9-digit field with preceding zeros.
  7. Read a string of the form "characters" from the standard input. Store the string in character array charArray. Eliminate the quotation marks from the input stream. Read a maximum of 50 characters (including the terminating null character).
15.7

Write a program to test the inputting of integer values in decimal, octal and hexadecimal formats. Output each integer read by the program in all three formats. Test the program with the following input data: 10, 010, 0x10.

15.8

Write a program that prints pointer values, using casts to all the integer data types. Which ones print strange values? Which ones cause errors?

15.9

Write a program to test the results of printing the integer value 12345 and the floating-point value 1.2345 in various-sized fields. What happens when the values are printed in fields containing fewer digits than the values?

15.10

Write a program that prints the value 100.453627 rounded to the nearest digit, tenth, hundredth, thousandth and ten-thousandth.

15.11

Write a program that inputs a string from the keyboard and determines the length of the string. Print the string in a length that is twice the field width.

15.12

Write a program that converts integer Fahrenheit temperatures from 0 to 212 degrees to floating-point Celsius temperatures with 3 digits of precision. Use the formula

celsius = 5.0 / 9.0 * ( fahrenheit - 32 );
 

to perform the calculation. The output should be printed in two right justified columns and the Celsius temperatures should be preceded by a sign for both positive and negative values.

15.13

In some programming languages, strings are entered surrounded by either single or double quotation marks. Write a program that reads the three strings suzy, "suzy" and 'suzy'. Are the single and double quotes ignored or read as part of the string?

15.14

In Fig. 11.5, the stream extraction and stream insertion operators were overloaded for input and output of objects of the PhoneNumber class. Rewrite the stream extraction operator to perform the following error checking on input. The operator>> function will need to be reimplemented.


  1. Input the entire phone number into an array. Test that the proper number of characters has been entered. There should be a total of 14 characters read for a phone number of the form (800) 555-1212. Use ios_base-member-function clear to set failbit for improper input.
  2. The area code and exchange do not begin with 0 or 1. Test the first digit of the area-code and exchange portions of the phone number to be sure that neither begins with 0 or 1. Use ios_base-member-function clear to set failbit for improper input.
  3. The middle digit of an area code used to be limited to 0 or 1 (although this has changed recently). Test the middle digit for a value of 0 or 1. Use the ios_base-member-function clear to set failbit for improper input. If none of the above operations results in failbit being set for improper input, copy the three parts of the telephone number into the areaCode, exchange and line members of the PhoneNumber object. In the main program, if failbit has been set on the input, have the program print an error message and end, rather than print the phone number.
15.15

Write a program that accomplishes each of the following:

  1. Create a user-defined class Point that contains the private integer data members xCoordinate and yCoordinate and declares stream insertion and stream extraction overloaded operator functions as friends of the class.
  2. Define the stream insertion and stream extraction operator functions. The stream extraction operator function should determine whether the data entered is valid, and, if not, it should set the failbit to indicate improper input. The stream insertion operator should not be able to display the point after an input error occurred.
  3. Write a main function that tests input and output of user-defined class Point, using the overloaded stream extraction and stream insertion operators.
15.16

Write a program that accomplishes each of the following:

  1. Create a user-defined class Complex that contains the private integer data members real and imaginary and declares stream insertion and stream extraction overloaded operator functions as friends of the class.
  2. Define the stream insertion and stream extraction operator functions. The stream extraction operator function should determine whether the data entered is valid, and, if not, it should set failbit to indicate improper input. The input should be of the form

    3 + 8i
    
  3. The values can be negative or positive, and it is possible that one of the two values is not provided. If a value is not provided, the appropriate data member should be set to 0. The stream-insertion operator should not be able to display the point if an input error occurred. For negative imaginary values, a minus sign should be printed rather than a plus sign.
  4. Write a main function that tests input and output of user-defined class Complex, using the overloaded stream extraction and stream insertion operators.
15.17

Write a program that uses a for statement to print a table of ASCII values for the characters in the ASCII character set from 33 to 126. The program should print the decimal value, octal value, hexadecimal value and character value for each character. Use the stream manipulators dec, oct and hex to print the integer values.

15.18

Write a program to show that the getline and three-argument get istream member functions both end the input string with a string-terminating null character. Also, show that get leaves the delimiter character on the input stream, whereas getline exTRacts the delimiter character and discards it. What happens to the unread characters in the stream?

Introduction to Computers, the Internet and World Wide Web

Introduction to C++ Programming

Introduction to Classes and Objects

Control Statements: Part 1

Control Statements: Part 2

Functions and an Introduction to Recursion

Arrays and Vectors

Pointers and Pointer-Based Strings

Classes: A Deeper Look, Part 1

Classes: A Deeper Look, Part 2

Operator Overloading; String and Array Objects

Object-Oriented Programming: Inheritance

Object-Oriented Programming: Polymorphism

Templates

Stream Input/Output

Exception Handling

File Processing

Class string and String Stream Processing

Web Programming

Searching and Sorting

Data Structures

Bits, Characters, C-Strings and structs

Standard Template Library (STL)

Other Topics

Appendix A. Operator Precedence and Associativity Chart

Appendix B. ASCII Character Set

Appendix C. Fundamental Types

Appendix D. Number Systems

Appendix E. C Legacy Code Topics

Appendix F. Preprocessor

Appendix G. ATM Case Study Code

Appendix H. UML 2: Additional Diagram Types

Appendix I. C++ Internet and Web Resources

Appendix J. Introduction to XHTML

Appendix K. XHTML Special Characters

Appendix L. Using the Visual Studio .NET Debugger

Appendix M. Using the GNU C++ Debugger

Bibliography



C++ How to Program
C++ How to Program (5th Edition)
ISBN: 0131857576
EAN: 2147483647
Year: 2004
Pages: 627

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net