Topical Information

Here are a few questions to help you clarify your understanding of console input/output.

Quiz Information

Questions

  1. What header file contains the definitions for console input and output?

    
            iostream
    
    
    

    Explicitly what items are missing if you fail to include this header?

    
            cout, cin, cerr, endl, ws, <<, >>, .peek(), .ignore(),
            .clear(), .get(), .flush(), etc.
    
    
  2. The C++ name for the screen is cout. Every item to be output should be preceded by the insertion operator (represented by the << symbol).

  3. The C++ name for the keyboard is cin. Every item to be input should be preceded by the extraction operator (represented by the >> symbol).

  4. Items being read from the keyboard are normally expected to be separated by spaces, new-lines, or tabs (any kind of white space in general).

  5. However, we can make clever use of the rules for numbers to mix input of character type data with/amongst numeric input to make the user's experience more natural. (For instance with dice — 3d4 or time — 12:34 or points in space — (3,4) etc.)

  6. Normally cout doesn't empty its boat (more correctly called a buffer) until it becomes full. Name at least two other situations that also cause cout to print all of its 'cargo' to the screen.

    
            
    • the program ends
    • cin wants to 'read'
    • endl or flush are inserted or .flush() is called
  7. cin also sees the keyboard as a boat full of typed characters. Each boat full will end in a new-line ('\n') character (since the user must signify the end of their input by hitting a certain key...).

  8. As your program requests the extraction of data from the keyboard into variables, cin will do its best to translate the incoming character stream into the proper type of data. For instance, the input stream containing 12/48 could be directly interpreted by cin as:

    1. '1', '2', '/', '4', '8' YES
    2. 12, '/', 48 YES
    3. '1', 2, '/', '4', 8 YES
    4. 12.0, '/', 48.0 YES
    5. 12, 48 NO
    6. 0.25 NO

    given the proper translation requests. (No extra code! Just a cin extraction statement!)

  9. In fact, why not show me how those that you chose above could be 'translated' in a program? Show only the declaration of any necessary variables and the cin statement that would perform the translation. (You need not do anything for translations that could not actually work.)

    
        
    1.     char a, b, c, d, e;
          cin >> a >> b >> c >> d >> e;
    2.     char a;
          short b, c;
          cin >> b >> a >> c;
    3.     char a, b, c;
          short d, e;
          cin >> a >> d >> b >> c >> e;
    4.     char a;
          double b, c;
          cin >> b >> a >> c;
  10. An earlier version of the C++ standard made cin behave like the standard input of our ancestral C language. Newer versions of the standard became more sane. However, the older behavior may still be in effect in some compilers. To be able to watch for this, we must understand what it would once have done. In these once-compliant compilers, cin decides the base of a number by the first characters it sees. A leading 0 makes the value octal (base eight (8)). Unless, of course, there is a x after it. Then it is hexadecimal (base sixteen (16)). Otherwise, it assumes the value is decimal (base ten (10)).

    To force cin to accept a specific base instead of deciding automatically, we can use the base manipulators from the iomanip library. Name the three manipulators and state the number base each represents.

    
            dec  decimal
            oct  octal
            hex  hexadecimal
    
    
  11. When this translation fails, however, cin becomes terribly upset and refuses to translate further. To detect this and attempt to fix it, we can use the .fail() function (owned by cin).

    A typical loop to attempt to fix failed translation might look like:

    
            // prompt...
            cin >> number;
            while ( cin.fail() )
            {
                cin.clear();
                cin.ignore( numeric_limits<streamsize>::max(), '\n' );
                // error message?
                // re-prompt?
                cin >> number;
            }