15.6 |
Write a statement for each of the following:
- Print integer 40000 left justified in a 15-digit field.
- Read a string into character array variable state.
- Print 200 with and without a sign.
- Print the decimal value 100 in hexadecimal form preceded by 0x.
- 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.
- Print 1.234 in a 9-digit field with preceding zeros.
- 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.
- 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.
- 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.
- 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:
- 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.
- 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.
- 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:
- 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.
- 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
- 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.
- 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?
|