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:
- Unformatted console input/output
- Formatted console input/output
Unformatted console input/output
1.Input stream
- It read the operation through keyword.
- It uses cin object.
Syntax:
cin >> variable;
Example:
int age; cin >> age;
2.Output stream
- It displays the output in the monitor or screen.
- It uses cout object.
Syntax:
cout << variable;
Example:
int age; cout << age;
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:
get(char); get(void);
4.getline() and write() functions
- getline()-reads a whole line of text that ends with the newline(‘\n’)
Syntax:
cin.getline (line,size);
- write()-displays the entire line input by the programmer
Syntax:
cout.write (line,size);
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
Functions | Used 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:
cout.width(n);
2.precision()
This function is used to set the floating point of the output.
Syntax:
cout.precision(d);
3.fill()
This function is used to set a character to fill in the blank space of the field.
Syntax:
cout.fill(ch);
4.setf()
This function is used for setting various flags for formatting output.
Syntax:
cout.setf(arg1,arg2);
4.unsetf()
This function is used for removing the flag setting.