what is formatted and unformatted data in C++

Follow by Email
Facebook0
Facebook
Google+
https://codingsec.net/2016/10/formatted-unformatted-data-c/

Formatted and Unformatted Data

 

Data which is received by the program without any modifications and sent to the output device without any modifications is known as unformatted data. On the other hand, sometimes we may want to apply some modifications to the actual data that is being received or sent. For example, we might want to display an integer in hexadecimal format in the output, leave some whitespace when printing a number and adjustments in the decimal point. Such modified data in known as formatted data.

 

As an example for formatted data, if we want to display a decimal number in hexadecimal format, we can use the hex manipulator as shown below:

 

cout<<hex<<15

 

Above line displays 15 in hexadecimal format as F.

 

Unformatted Console I/O

 

The input stream uses cin object of istream class to read data and the output stream uses coutobject of ostream class to display data on output devices.

 

The cin statement uses >> (extraction operator) to read data from keyboard. Syntax of cin statement is shown below:

 

cin>>variable-name;

 

Example:

 

In the above example, cin is used in cascading fashion. This is helpful for reading values into multiple variables in a single statement. No need of any format specifiers like %d, %f etc. The >> (extraction) operator will take care of that automatically.

 

If the data provided is greater than the number of variables in the cin statement, extra data will remain in the input stream.

 

The cout statement uses << (insertion operator) to display data to the output device. Syntax of cout statement is shown below:

 

cout<<variable-name;

 

Example:

cout<<a<<b<<c;

 

In the above example, a, b, and c are variables. No need to specify format specifiers like %d, %f, etc. The << (insertion) operator will take care of that automatically. All escape sequences like “\n”, “\t”, etc, can be used in cout statement.

 

 

To read a single character, we can use get() function of istream class and to print a single character, we can use put() function of ostream class. Syntax of these methods is as follows:

 

get(char)

get(void)

put(char)

 

 

To read a string or a line of text, we can use getline() function and to print a string we can use write()function. Syntax of both of these methods is as follows:

 

getline(string, size)

write(string, size)

 

The size in the above syntax specifies the number of characters to read and print in the given string.

 

 

istream Class Functions

 

The istream class is derived from ios class. The istream class contains the following functions:

istream-functions-1

Formatted Console I/O

 

C++ provides various console I/O functions for formatted input and output. The three ways to display formatted input and output are:

 

  1. ios class functions and flags
  2. Standard manipulators
  3. User-defined manipulators

 

ios Class

 

The ios class is the base class for all input and output classes in C++. Following are some of the functions available in ios class:

ios-functions-1

Follow by Email
Facebook0
Facebook
Google+
https://codingsec.net/2016/10/formatted-unformatted-data-c/

Add a Comment

Your email address will not be published. Required fields are marked *

Like the article? please consider sharing it. Thank you

Advertisment ad adsense adlogger