Formatted and Unformatted I/O Console

Unformatted data

  • The printed data with default setting by the I/O function of the language is known as unformatted data.
  • It is the basic form of input/output and transfers the internal binary representation of the data directly between memory and the file.
  • For example, in cin statement it asks for a number while executing. If the user enters a decimal number, the entered number is displayed using cout statement. There is no need to apply any external setting, by default the I/O function represents the number in decimal format.

Formatted data

  • If the user needs to display a number in hexadecimal format, the data is represented with the manipulators are known as formatted data.
  • It converts the internal binary representation of the data to ASCII characters which are written to the output file.
  • It reads characters from the input file and coverts them to internal form.
  • For example, cout<<hex<<13; converts decimal 13 to hexadecimal d. Formatting is a representation of data with different settings (like number format, field width, decimal points etc.) as per the requirement of the user.

Input/Output Streams

  • The iostream standard library provides cin and cout object.
  • Input stream uses cin object for reading the data from standard input and Output stream uses cout object for displaying the data on the screen or writing to standard output.
  • The cin and cout are pre-defined streams for input and output data.
    Syntax:
    cin>>variable_name;
    cout<<variable_name;
  • The cin object uses extraction operator (>>) before a variable name while the cout object uses insertion operator (<<) before a variable name.
  • The cin object is used to read the data through the input device like keyboard etc. while the cout object is used to perform console write operation.

Example: Program demonstrating cin and cout statements

#include<iostream>
using namespace std;
int main()
{
        char sname[15];
        cout<<"Enter Employee Name : "<<endl;
        cin>>sname;
        cout<<"Employee Name is : "<<sname;
        return 0;
}


Output:
Enter Employee Name : Prajakta
Employee Name is : Prajakta

In the above example, cout<<"Employee Name is : "<<sname displays the contents of character array sname (student name). The cout statement is like printf statement as used in C language.

The cin statement cin>>sname reads the string through keyboard and stores in the array sname[15]. The cin statement is like scanf statement as used in C language. The endl is a manipulator that breaks a line.

Typecasting

Typecasting is a conversion of data in one basic type to another by applying external use of data type keywords.

Example: Program for Typecasting and displaying the converted values

#include<iostream>
using namespace std;
int main()
{
        int i = 69;
        float f = 4.5;
        char c = 'D';
        cout<<"Before Typecasting"<<endl;
        cout<<"---------------------------------------------"<<endl;
        cout<<"i = "<<i<<endl;
        cout<<"f = "<<f<<endl;
        cout<<"c = "<<c<<endl<<endl;
        cout<<"After Typecasting"<<endl;
        cout<<"--------------------------------------------"<<endl;
        cout<<"Integer(int) in Character(char) Format : "<<(char)i<<endl;
        cout<<"Float(float) in Integer(int) Format : "<<(int)f<<endl;
        cout<<"Character(char) in Integer(int) Format : "<<(int)c<<endl;
        return 0;
}


Output:
Before Typecasting
---------------------------------------------
i = 69
f = 4.5
c = D

After Typecasting
--------------------------------------------
Integer(int) in Character(char) Format : E
Float(float) in Integer(int) Format : 4
Character(char) in Integer(int) Format : 68

In the above example, the variables of integer(int), float(float) and character(char) are declared and initialized i = 69, f = 4.5, c = 'D'.

In first cout statement, integer value converted into character according to ASCII character set and the character E is displayed.

In second cout statement, float value is converted into integer format. The displayed value is 4 not 4.5, because while performing typecasting from float to integer, it removes decimal part of float value and considers only integer part.

In third cout statement, character converted into integer. The value of 'D' is 69, printed as an integer. The integer format converts character into integer.

typecasting integer

In the above diagram, 69 is an integer value and it is converted into character E by using typecasting format (char).