Topical Information

Use the String class that we developed in lecture to solve the following assignment.

Program Information

Having added translation for C-strings and overflow protection thereof and insertion for all 6 built-in types, Cstring, and even String, what abilities can our String class now claim to have? Why it can now set width on inserted items, of course!

Unfortunately, it isn't automatic...you'll have to show it how.

Adjust your code to allow your insertion operators to account for the width a user set. (You can even have it work on char data -- unlike the stream's inane version.)

Don't forget to create a test app for your new modifications (perhaps by adding them to the String test application) to show your code works properly.

Thought Provoking Questions

  1. Is it difficult to have the width checked for char, Cstring, or String items?

  2. How about for integers? (Hint: Remember that formula with the log's and floors?)

  3. Can you extend your integer solution easily to decimal numbers? (Remember, you are still deciding the default precision!)

  4. Will you modify things in your own class or in the auxiliary functions? Both? Why/Why not?

  5. What character will you fill with when they've given you data which is too short?

  6. What do you do if their data is too long? C/C++ let the data through unchanged. Our String class cut itself off appropriately. Fortran will print '*' characters for the width of the field to show the actual value was too wide. What will your String class' insertion do?

This assignment is (Level 2).

Options

Add (Level 0.5) to allow the user of your class to specify the fill character.

Add (Level 1.5) to allow the user to set the policy on how to handle data too long for the specified width. (I'd recommend stars, ignore_width, or truncate as good possible policies.) (Hint: This is best done through flags. See how the String class already handles the case_on flag in both its use and management.)

Add (Level 0.5) to allow the user of your class to specify the precision of decimal values (still in fixed notation with a showpoint-like effect in place).

Add (Level 1) to allow the user of your class to specify the justification within the field: left, right, internal, or center. Here's a 'clip' from the ios class to give you some ideas:

    enum { left = 1,
           right = 2,
           internal = 4,
           adjustfield = 7 };

(Also see our code for ostream &operator<< for width handling...)

More Thought Provoking Questions