Topical Information

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

Question Set 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, <<, >>, .peek(), .ignore(),
            .clear(), 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 is inserted
  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. There are [essentially] three ways a newline may appear during the run of a C++ program. The first is when the user hits Enter (Return) when interacting with a cin statement. The second is when the programmer has inserted (operator <<) to the screen a newline escape within either single or double quotes. And the third is when the programmer has inserted to the screen a(n) endl manipulator.

    The first is noteworthy in as it affects our user interface design in a rather unique way. But the other two seem to be more obvious and yet have subtle differences that can sometimes lead to confusion as to what has happened during a program.

    The first difference is that the raw newline character must be within some form of quotes and the endl command cannot be within quotes. Luckily, when this effect is felt, it is quite obvious and easy to fix.

    The second difference is less obvious and doesn't even always cause issues. This makes it harder to understand and fix the intermittent issues. The thing is that the display of a raw newline escape will not take place immediately. It will wait until one of the various conditions mentioned above causes cout to display. The endl, on the other hand, will immediately cause cout to display on-screen. This difference may cause lag in a program's display — or may not. Again, it is hard to detect sometimes. It is very situational.

  11. 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