Data Input and Output in C++
Data Input and Output in C++
C++ supports a rich set of I/O functions for performing I/0 operations. It can also use all the I/0 functions of C language in the C ++ program. But we normally avoid use of C's I / O functions like scanf and printf etc. Because of their inefficiency to handle errors properly, not flexible enough to handle user defined datatypes such as classes and in the worst case they are not object oriented at all.
C ++ STREAMS
C ++ provides a new technique for handling I / O operations through a mechanism known as streams. A stream is an abstraction that refers to the flow of data. The flow of data may be from an input device (keyboard, disk file etc.) to a program in main memory or from a program to any output device (monitor, printer etc.)
Streams can be classified into two categories:
a) Output Stream
b) Input Stream
In output stream, the flow of data (bytes) is from the program to the output device. Output operations are carried out using output stream in C ++.
In input stream, the flow of data is from the input device to the program in main memory In C ++, input operations are carried out using input stream.
Stream acts as an intermediator between a wide variety of I / O devices and program by providing a consistent and device independent interface to the programmer.
The I / system of C language is not flexible enough as in C ++. In C, the syntax for using TO functions differ depending on the device used i.e. inputting from keyboard or disk files require different functions (Eg. scanf (), fscanf ()). But in C ++, using the notion of streams, the VO operations are carried out using the same syntax irrespective of device used. For example - Same function can be used to write on to the screen as well as on to the disk file or printer.
C ++ has a number of pre-defined standard I / O streams that are linked to standard I / O devices like Keyboard. Monitor, Printer etc.
These streams are automatically opened when the execution of the program starts. The pre-defined standard I / O streams includes cin, cout, clog and cerr objects. The standard input and output operations for a program are performed by stream object's cin for Input and cout for Output. Moreover, cerr and clog objects are two output streams which are normally used to display error messages.
To perform standard I / O operations in C ++, it is necessary to include iostream.h header file at the top of program. The iostream.h header file contains the information that C ++ requires to perform standard I / O operations using cin, cout etc.
OUTPUT USING cout
The cout is used to display a variable, constant or expression onto the standard output device i.e. monitor.
In C ++, the standard output stream is represented using pre-defined object cout (pronounced as see - out), so as to perform standard output operation. The cout stream is used in conjunction with bitwise left shift operator (<<) for performing standard output operations.
The syntax for the standard output stream operation is
cout << data ; // data flow in the direction of arrow
where data can be a variable, expression, user-defined datatypes or a constant. In the cout statement, cout is followed by the insertion or put to operator (<<) and then followed with data items which are to be displayed. The operator («<) is called the insertion or put to operator as it inserts the data that follows it into the cout stream which further directs the contents to the display screen.
Now, consider the following statement,
cout << "Hello Everybody";
This causes the string constant Hello Everybody to be displayed on the screen. The insertion operator («<) inserts the string constant into the standard output stream cout which further sends it to the screen.
Some other valid output statements are –
Qcout << 'K'; // Displays the character K
cout << total; // Displays the contents of predefined // variable 'total' of type float
cout << 350; // Displays the constant number 350.
cout << 101.756 . // Displays the number 101.756
cout << '\ n'; // Move the cursor to next line
cout << a + b; 7 // Displays the result of expression a + b.
The insertion (<<) can be used more then once in a same cout statement. Such output operations are called chaining or cascaded output operations. The main advantage of repeating the insertion operator is to display a combination of variables, constants and expressions in the same statement.
Consider the example,
cout << "Result of a + b. =" << c;
The above statement is an example of cascaded output operation in which the insertion operator (<<) is used more than once in the same statement using cout object. This statement displays the message result of a + b = followed by value stored in a variable e. So the Output is
Result of a + b = 30 (Assume that variable c certain value 30)
Instead of using multiple cout statements, the same output is achieved using a single cout statement by cascading insertion operator as discussed. There is no restriction on the maximum number of data items to be displayed in a same cout statement.
cout<<var1<<var2<<..........<<varn;
To explain the different uses of cout for performing output operations, let us consider the following program
Output |
The escape sequence "\ n 'used in the above program moves the cursor to the next line In the above program the cout statements display values of different datatypes. For example, the statement
cout << age <<'. ‘<< salary ;
displays The values of age and salary variables which are of datatypes int and float respectively are moreover, there is no need to specify the data types in the output statement as in C language where% d and% f are used (like printf("%d%f",age, salary);). Therefore, I / O statements are generally easier to write and use in C ++.
INPUT USING cin
The cin is used to input a number, a character or a string of characters from a standard input device ie keyboard.
The standard input stream in C ++ is represented using pre - defined object cin (pronounced as see - in) so as to perform standard input operations. The cin stream is used in conjunction with bitwise right shift operator (>> ) for performing standard input operations. The syntax for t he standard input stream operation is
cin >> variable; // Data flow in the direction of arrow
where variable can be of any basic or user defined data type. In the cin statement, cin is followed by the extraction or get from operator (>>) and then followed with variable into which the input data is to be stored. The operator (>>) is called extraction or get from operator as it extracts the data from standard input stream cin connected to standard input device (keyboard) and sends the data into the variable that follows it.
Now, consider the following statements,
Int x ;
Cin>>x ;
This input statement accepts an integer value from keyboard and stores in the variable x Some other valid input statements are:
cin >> per; // Reads float value
cin >> sex; // Reads character value
cin >> volume; // Reads double value
where per, sex, volume are the variables of type float, char and double respectivily. Now, let us consider a program to calculate the area of square.
In the above program on execution , the cin statement waits from the user to input a integer value from the keyboard and the value is stored in variable 1 after pressing the enter key . After calculating the area of the square , the cout statement displays the area .
Like insertion operator ( << ) , the extraction operator ( >> ) can also be used more than once in the same cin statement for inputting the data into variables of same or different datatypes . Such input operations in C ++ are called cascaded input operations . For Example - To input age , sex and per of a student using the same cin statement can be performed as follows.
cin >> age >> sex >> per;
Here age, sex, per are variables of type int, char, float respectively. The order of reading the values of the variables is from left to right. Thus in the above cin statement, it first reads the age of student, followed by its sex and then the percentage.
Leave a Comment