Topical Information

The purpose of this quiz is to give you a chance to focus your knowledge of formatting stream input/output in C++.

Quiz Information

Questions

  1. Show the code necessary to center the string "Hello World" on a typical line of the screen (80 characters wide).

    
        cout << setw((80+11)/2) << "Hello World" << '\n';
    
    

  2. The flags _____ (for padding a field to the left), _____ (for padding a field to the right), and ____ (for separating the sign and value and padding in the center) should not all (or indeed any pair) be set on at once.

    1. ios_base::left, ios_base::right, ios_base::center NO
    2. ios_base::left, ios_base::right, ios_base::internal NO
    3. ios_base::right, ios_base::left, ios_base::center NO
    4. ios_base::right, ios_base::left, ios_base::internal YES
    5. None of the above. NO

  3.  
    TRUE   FALSE   Most formatting items are (semi-)permanent (in that they won't change until you change them).
    TRUE   FALSE   There is, in fact, only one format setting that is completely temporary (lasting only as long as the next output item).
    TRUE   FALSE   That format setting is the fill character.
  4. The width of the next output item is set by the _____ output stream method. One can also use the _____ stream manipulator to set an output field's width.

    1. setf, setiosflags NO
    2. fill, setfill NO
    3. width, setw YES
    4. precision, setprecision NO
    5. unsetf, resetiosflags NO

  5. More esoteric flags include the ability to have streams recognize bool data as words (boolalpha), the forcing of all base designations to be printed (showbase), and making bases, scientific Es, and such all print as uppercase letters (uppercase).

  6. Name at least four non-temporary format settings. (Hint: At least two of these must not be flags!) Explain (briefly) what each is responsible for.

    
        There are lots of options.  All flags are non-temporary -- there are more
        than a dozen of these -- and so are the precision and fill character
        settings.
    
        I'll pick the fixed and showpos flags and, of course, precision and fill.
    
        The fixed flag is to tell any decimal numbers to keep their decimal point
        fixed after the 1s place -- not to float around like in scientific notation.
    
        The showpos flag is to force non-negative integers to show a + sign before
        them -- even 0!
    
        precision's effect changes depending on whether we are in fixed, scientific,
        or default mode.  It's rules are rather complicated, but basically it amounts
        to precise digits in scientific mode or decimal places in fixed mode.
    
        The fill character is used when a width is set to make sure the next output
        item is padded out to the width should the data fall short of the width itself.
        Whatever char is in the fill character is used for this padding.
    
    

  7. Show stream-oriented code to draw a line of 60 tilde (~) characters.

     
    
        char old_fill = cout.fill('~');
        cout << setw(60) << "~" << '\n';
        cout.fill(old_fill);
    
    

  8. How would you draw a hollow box (* for border — center blank) 50 character wide and 13 characters tall in a stream-oriented fashion? Show the code here:

    
        char old_fill = cout.fill('*');
        cout << setw(50) << "*" << '\n';
        cout.fill(' ');
        for (short l = 2; l <= 12; ++l)
        {
            cout << '*' << setw(49) << "*" << '\n';
        }
        cout.fill('*');
        cout << setw(50) << "*" << '\n';
        cout.fill(old_fill);
    
    

  9.  
    TRUE   FALSE   A function which alters format settings on a stream should always preserve the caller's original settings for restoration before returning.
    TRUE   FALSE   Toward this end, all formatting functions return the value just set.
  10. All formatting flags begin with ios_base::. The reason for this is that they are members of a class called ios_base.

  11.  
    TRUE   FALSE   Formatting can be applied to both file streams and the console streams (as appropriate).