Iostream Library and Basic I/O in C++ - Basic I/O in C
(Page 3 of 4 )
What each of the objects I mentioned at the end of the last section mean is quite simple. The cin is assigned to the input device of the system (stdin), while the cout does the same in the output direction (stdout). The two that remain are reserved for error reporting (stderr). While cerr offers an unbuffered version (though after every char a new flush), clog support is buffered, allowing the setting of a line of data through.
The first one is resolved instantly, as there is no space for storing the problem, while the second can be resolved afterward. The clog is very rarely used; this is why it tends to be ignored.
Now here is a little demonstration of how this will work in real life coding. Before you use them, you need to include theiostreamlibrary, and unless you want to point out at every step that this is a member of the STL by calling functions like std::cin/std::cout, you should also signal that we'll be using the STL namespace.
#include<iostream>
using namespace std;
With the cin stream, the extraction operator will be used; that will be followed by what to extract as input.
int justANumber;
cin >> justANumber;
With cout, the method is similar; we will insert something into the cout stream with the insertion (<<) operator. In addition, here we should mention that the end line is composed of more than one character. You probably know that in C you used the 'n' char (backslashfollowed by the lettern), which stands for new line.
Nevertheless, whenever you hit an "enter" key, let us say in notepad, there will be an additional carriage return as well; that is represented by the 'r' character (backslashfollowed by the letterr). Therefore, in theiostreamlibrary, a constant contains all this and will flush the stream(std::endl). By this, we mean that it will make sure that the data are sent to the screen and are no longer waiting to be displayed.
Sometimes this behavior is not desired. Since flushing the stream takes up a few processes, you may not want to print the result of every step on the screen; you may prefer to do it once at the end to speed up the execution time of the application. In these cases, avoid using std::endl --instead, write on the screen the character sequence "nr" (new line + carriage return).
cout << justANumber << std::endl;
As you see, you can add multiple items to the stream in a single line. This is possible because the extraction/insertion operator is also returning a reference to the stream, so in fact the upper lines look like this:
(cout.operator<<(justAnumber))/*=cout*/.operator<<(std::endl);
Here we arrive at the question of what will happen once you enter invalid data, such as text when you are expecting a number. Let's experiment.
#include <iostream>
int main()
{
int one, two;
std::cin >> one >> two;
std::cout.operator <<(one).operator <<(two).operator <<(std::endl); // == cout << one << two << endl;
return 0;
}
With results:
123AB
123-858993460
Press any key to continue . . .
AB123
-858993460-858993460
Press any key to continue . . .
From this, you may already know the answer. It will extract any valid data it can. Once an invalid input is received, the fail flag of the stream is set, and any future work with the stream will fail to complete.
It is your duty to anticipate this kind of behavior and control it, or use it to your advantage. You may create a while loop to enter an indefinite number of rows of int until invalid input is entered:
while(cin>> justANumber)
{
// do whatever you want with the input.
}
As I said before, the insertion operator will return the stream itself. In conclusion, after the insertion is complete, the loop will evaluate whether the statement between the parentheses is or is not true. In the case of a stream, this is reduced to the evaluation of the fail flag of the stream.
However, if you want to enter a char sequence after this, you need to reset the stream by calling the clear function.
if(std::cin.fail())
std::cin.clear();
//now we can resume the input operation
You may also choose to ignore some chars if you know what is causing the problem, using the ignore function:
cin.ignore(2); // ignore the first two character of the input
Next: Conclusion >>
More C++ Articles
More By Gabor Bernat