Formatting Your Output with C++ Streams
If you do a great deal of C++ programming, sooner or later you'll end up having to format some text data. This might be the case if you're writing text to the screen (such as in a console program) or if you're writing text to a file.
Thanks to the beauty of an important part of the C++ standard called streams, you can easily format your text so it lines up appropriately, your floating point numbers have the precision you need, your numbers are right- or left-justified, your tables have columns that look nice, and so onregardless of whether you're writing to the console or to a file. In this article, I'll show you how to do all this.
I assume that you're familiar with the basic idea of using cout in your programs to write text to the console. From this notion, I'll build on how you can format your text nicely. But first I need to talk about an issue that comes up again and again in C++ programming: the ANSI Standard Issue.
ANSI Standard Issue
Back in 1998, ANSI released its official standard for the C++ programming language.
Note: Actually, it's ISO that released the standard. Officially, it ratified an ANSI standard that had been approved a year earlier, in 1997. The current standard is therefore an ANSI/ISO standard or simply an ISO standard.
This standard included a huge set of classes and functions known as the C++ Standard Library, which is where you find all the stream classes.
To get the most out of the language, the standards committee agreed to lump all the classes in the Standard Library into a single namespace called the std (which stands for standard) namespace.
In earlier versions of C++, if you wanted to use the streams for doing things such as writing to the console, you would include the header file iostream.h, like so:
Then, if you want to write text to the console, you would have code such as the following, which would write code to the console:
cout << "Hello" << endl;
But the ANSI standard replaces this with a new file that doesn't have the .h extension, like so:
The idea here is that compilers can support both the new ANSI header files and the older ones. But if you want to use the new ANSI headers, the cout and endl identifiers as well as all the other features of the streams are in the std namespace. Thus, just attempting a cout << "Hello" << endl; will result in an error. Instead, you have to fully qualify the identifiers from the standard namespace, as follows:
std::cout << "Hello" << std::endl;
Frankly, that's a pain. Who wants to do that? So instead of typing std:: over and over again, you can put the following line of code after your #include lines:
using namespace std;
That tells the compiler that if it can't find an identifier (such as cout), to then try looking in the std namespace. Thus, this good old line will compile just fine:
cout << "Hello" << endl;
The compiler won't at first find the cout identifier, and so the compiler will in turn look in the std namespace, where it will find it. The end result of all this is that in this article, I'm assuming that you'll be using the new ANSI headers; however, in my samples I'll include the using namespace std; line. And now back to our regularly scheduled program!
As a compromise between these two approaches, one can use a using declaration:
using std::cout; using std::endl; cout << "Hello" << endl;//now fine