C++ Input/Output

C++ Input/Output function takes input from the standard input device and compute and then gives the computed result to the standard output device.

Also, the standard input device is keyword and the standard output device is monitor.

Input/Output in C++ operations are performed under the iostream class in standard input devices or standard output devices(generally keyword and monitor).

The standard input/output functions in C++ for standard library iostream are:

  • cin
  • cout

The basic input/output console operations are divided into two types:

  1.  Unformatted console input/output
  2. Formatted console input/output

Unformatted console input/output

1.Input stream

  • It read the operation through keyword.
  • It uses cin object.

Syntax:

Example:

2.Output stream

  • It displays the output in the monitor or screen.
  • It uses cout object.

Syntax:

Example:

3.get() and put()

  • get() and put() functions in C++ are used to input and output single character.
  • get()-reads the character
  • put()-displays the character

Also,  get() has two syntax:

4.getline() and write() functions

  • getline()-reads a whole line of text that ends with the newline(‘\n’)

Syntax:

  • write()-displays the entire line input by the programmer

Syntax:

Formatted console input/output

C++ provides various formatted console I/O functions for formatting the output.

  • User-defined output functions
  • ios class functions and flags
  • Manipulators
FunctionsUsed For
width()To set required field width. Displays the output with a given width.
precision()Used to set a floating point of the output.
fill()To set a  character to fill in the blank space of the field.
setf()To set various flags for formatting output.
unsetf()To remove the flag setting.

1.width()

It is used to set the width of the output.

Syntax:

2.precision()

This function is used to set the floating point of the output.

Syntax:

3.fill()

This function is used to set a character to fill in the blank space of the field.

Syntax:

4.setf()

This function is used for setting various flags for formatting output.

Syntax:

4.unsetf()

This function is used for removing the flag setting.

 

 

 

 

 

 

SHARE C++ Input/Output

You may also like...

Leave a Reply

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

Share