Input and Output with stdin and stdout

Basic input and output to your monitor and from your keyboard is essential to your programs. How else, at the beginning, are you going to get parameters and data into, and results out of, your program? The iostream header file is included in all Ebe templates to include basic input and output functions like cin, cout, get, peek, putback and ignore. The include statement looks like this:

    #include <iostream>

Note the absence of a semicolon at the end of this statement. Iostream makes use of the std namespace, so the following line must also be part of the basic file template:

    using namespace std; 

Now we can use cin and cout to input data to our programs and output results. Get, peek, putback, ignore, and clear are also available via iostream. Example code snippets for these I/O functions are available within this section. The basic cout/ cin pair for a user prompt for input looks like this:

   cout << "Type in a value for degrees in Fahrenheit, then press enter:  " << endl;
   cin >> fahrDegrees;

Note the stream insertion operator << in the cout statement and the endl. The << means, in this context, means place this token into the output stream. In this case, a literal sentence then a new line. The stream extraction operator, >>, from the cin statement, accepts the next token from the input stream (the keyboard, at this point), that corresponds to the variable, presumably declared above, fahrDegrees. The effect of the cin statement is that the keyboard input is read from the input buffer, where the keyboard placed it, and placed in the memory location pointed to by the variable name, fahrDegrees.

Additional functions, mentioned above, can perform many tasks with the input and output streams. These functions are capable of getting a single character, putting items back into the input buffer, looking ahead into it, ignoring portions of the input, and clearing the stream if an error occurs. Code snippets for all of these are included in the accompanying files. More specific cin and cout snippets can be found there, too.

I/O formatting is addressed in another set of code snippets. The ability to manipulate output to organize it into more readable formats is part of the header file iomanip. Iomanip provides the ability to set decimal places displayed, set display field widths, set justifications, and much more. This header file has an entire section dedicated to it.