Monday, June 18, 2012

Homework 3: String Manipulators

1. Write the necessary preprocessor directive to enable the use of the stream manipulators like setw and setprecision.

# include <iomanip>

2. Assume that  m is an int variable that has been given a value. Write a statement that prints it out in a print field of 10 positions.

cout << setw (10) << m ;

3. Assume that  x is a double variable that has been given a value. Write a statement that prints it out with exactly three digits to the right of the decimal point no matter what how big or miniscule its value is.

cout << setprecision ( 3) << fixed << x ;

4. Given three variables, k, m, n, of type int that have already been declared and initialized, write some code that prints each of them in a 9 position field on the same line. For example, if their values were 27, 987654321, -4321, the output would be:

 |xxxxxxx27987654321xxxx-4321

NOTE: The vertical bar,  | , on the left above represents the left edge of the print area; it is not to be printed out. Also, we show x in the output above to represent spaces-- your output should not actually have x's!

cout << setw (9) << k << setw (9) << m << setw (9) << n ;

No comments:

Post a Comment