Recapping basic I/O
In this section, we’ll briefly review simple character-based input and output with the keyboard and monitor. Simple manipulators will also be reviewed to both explain the underlying mechanics of I/O buffers and to provide basic enhancements and formatting.
The iostream library
One of the easiest mechanisms for input and output in C++ is the use of the <iostream>
library. The header file <iostream>
contains definitions of data types istream and ostream. Instances of these data types, cin
, cout
, cerr
, and clog
, are incorporated by including the std
namespace. The <iostream>
library facilitates simple I/O and can be used as follows:
cin
can be used in conjunction with the extraction operator>>
for buffered inputcout
can be used in conjunction with the insertion operator<<
for buffered outputcerr
(unbuffered) andclog
(buffered) can also be used in conjunction with the insertion operator, but for errors
Let’s review an example showcasing simple I/O:
#include <iostream> using namespace std; // we'll limit the namespace shortly int main() { char name[20]; // caution, uninitialized array of char int age = 0; cout << "Please enter a name and an age: "; cin >> name >> age; // caution, may overflow name var. cout << "Hello " << name; cout << ". You are " << age << " years old." << endl; return 0; }
First, we include the <iostream>
library and indicate that we’re using the std
namespace to gain usage of cin
and cout
(more on namespaces later in this chapter). Next, we introduce the main()
function, which is the entry point in our application. Here, we declare two variables, name
and age
, neither of which is initialized. Next, we prompt the user for input by placing the string "Please enter a name and an age: "
in the buffer associated with cout
. When the buffer associated with cout
is flushed, the user will see this prompt on the screen.
The keyboard input string is then placed in the buffer associated with cout
using the extraction operator <<
. Conveniently, one mechanism that automatically flushes the buffer associated with cout
is the use of cin
to read keyboard input into variables, such as seen on the next line, where we read the user input into the variables name
and age
, respectively.
Next, we print out a greeting of "Hello"
to the user, followed by the name entered, followed by an indication of their age, gathered from the second piece of user input. The endl
at the end of this line both places a newline character '\n'
into the output buffer and ensures that the output buffer is flushed – more of that next. The return 0;
declaration simply returns a program exit status to the programming shell, in this case, the value 0
. Notice that the main()
function indicates an int
for a return value to ensure this is possible.
Basic iostream manipulators
Often, it is desirable to be able to manipulate the contents of the buffers associated with cin
, cout
, and cerr
. Manipulators allow the internal state of these objects to be modified, which affects how their associated buffers are formatted and manipulated. Manipulators are defined in the <iomanip>
header file. Common manipulator examples include the following:
endl
: Places a newline character ('\n'
) in the buffer associated withcout
then flushes the bufferflush
: Clears the contents of the output streamsetprecision(int)
: Defines the precision (number of digits) used to output floating point numberssetw(int)
: Sets the width for input and outputws
: Removes whitespace characters from the buffer
Let’s see a simple example:
#include <iostream> #include <iomanip> using namespace std; // we'll limit the namespace shortly int main() { char name[20]; // caution; uninitialized array float gpa = 0.0; // grade point average cout << "Please enter a name and a gpa: "; cin >> setw(20) >> name >> gpa; // won't overflow name cout << "Hello " << name << flush; cout << ". GPA is: " << setprecision(3) << gpa << endl; return 0; }
In this example, first, notice the inclusion of the <iomanip>
header file. Also, notice that setw(20)
is used to ensure that we do not overflow the name variable, which is only 20 characters long; setw()
will automatically deduct one from the size provided to ensure there is room for the null character. Notice that flush
is used on the second output line – it’s not necessary here to flush the output buffer; this manipulator merely demonstrates how a flush
may be applied. On the last output line with cout
, notice that setprecision(3)
is used to print the floating point gpa
. Three points of precision account for the decimal point plus two places to the right of the decimal. Finally, notice that we add the endl
manipulator to the buffer associated with cout
. The endl
manipulator will first insert a newline character ('\n'
) into the buffer and then flush the buffer. For performance, if you don’t need a buffer flush to immediately see the output, using a newline character alone is more efficient.
Now that we have reviewed simple input and output using the <iostream>
library, let’s move forward by briefly reviewing control structures, statements, and looping constructs.