C++

Objectives :

  • C and C++ languages can take multiple inputs from the key board.
  • File Name : cin_stream1.cpp
    The cin object of C++ language can stream many strings separated with whitespaces as an input.
    The function "scanf("%i%i",&n1, &n2);" of C language, can also take multiple inputs separated by whitespaces.
  • The data-streaming in C++ has in built error handling schemes.
  • Code:cin_stream1.txt, test1.txt
 

Step 1: Create two source files, one for C and other for C++ languages respectively

Step: 2 Edit and save Source file

Step: 3 Runtime Views:
Testing C++ codes:
The illustration demonstrates that "cin" object with ">>" operator can take many strings separated with whitespaces.

Testing C codes

Step Brief Discussion:
We noticed that both C/C++ allow data-segregation, meaning that the input of data series will be stored in the same order as entered. The first data will be fetched to the first variable of the input stream, and so on. In C++language the object cin of istream with extraction operator, the incorrect data will generate an error and cin object will fail to entertain the input. As a result cout of ostream most likely display some unpredicted values.

In the above cases, while testing you codes, if you enter any character against integer, C compiler might crash, whereas C++ will terminate gracefully.

Testing Tolerance of iostream class of C++:  Note in the illustration below, I entered a string in the place of an integer, and as a result the application was terminated due to a data-mismatch-input error.

Testing the tolerance of C:
Enter some character against integer value as shown below and then press return. Note the command window crashed as a result of a mismatched data type.

I-O Condition states ( ref : http://www.icce.rug.nl/documents/cplusplus/ )
As we noted above, the stream operation may fail, and to prevent the crash the streaming operations will be suspended. This measure is most important aspect of data streaming operations of C++ language. In brief we will view some machineries of iostream.

The class ios_base is placed on the top of the hierarchies, of iostream libraries. The class ios which succeeds ios_base, it communicates other entities with few buffers, and streambuf is one of those buffers, which plays a key role in orchestrating input-output operation through all the associated objects. The overall operations rely on some triggers or conditional-flags, and few are described below.
ios::badbit: It is raised at an illegal operation, like matching failure (like integer vaiable gets alphabets), end of the file or the file does not exists.
ios::eofbit: It is raised when end of the file is reached.
ios::failbit: It is raised when operation could not be performed, due to wrong data precisions.
ios::goodbit: when data is good, and none of the above flags were raised.

There are other utilities associated with ios class, we will discuss those later.