C++ unformatted Input output operations
Overloaded Operators >> and << We have used the objects cin and cout (pre-defined in the iostream file) for the input and output of data of various types.
The following is the general format for reading data from the keyboard:
cin >> variable >> variable 2 >> . . . . >> variable
|
variable1, variable 2, . . . are valid C++ language variable names that have been declared already.
This above statement will cause the computer to stop the execution and look for input data from the keyboard by help of user. The input data
for this statement would be:
data1 data2 . . . . . . dataN
|
The input data are separated by white spaces and also should match the type of variable in the cin list. Spaces, newlines and tabs will be skipped.
in the above example operator >> reads the data character by character and assigns it to the indicated location. The reading for a variable only will be terminated at the encounter of a white space or a character that does not match the destination type.
For example, consider the following code:
suppose the following data is given as input:
42580
|
The operator will read the characters upto 8 and the value of code variable 4258 is assigned to the code. The character D remains in the input stream and will be input to the next cin statement. The general form for displaying data on the screen is:
cout << item1 << item2 << . . . . << itemN
|
The items item1 through itemN may be variables or constants of any basic type. We have used such
statements in a number of examples illustrated in previous chapters.