Input
You can enter several real variables from the input stream and write them into variables in the standard way:
float x, y;
cin >> x >> y;
The first number falls into the
\(x\) variable, the second into
\(y\) variable
Output
When outputting real numbers, the default is 6 decimal places, while the scientific format or with a fixed comma is automatically selected.
You can configure the output as needed by the condition of the task. For this, an additional
iomanip
library is used - manipulators that control the output.
The
fixed
command is used to output in fixed-point format, and
scientific
is used for the scientific format. Then you need to determine the number of digits in the fractional part using the
setprecision
command. Using the
setw
command, you can specify the total number of positions allocated to output the number
Example:
float x = 1.0/6;
cout << fixed << setprecision (9); // установили вывести 9 цифр в дробной части
cout << setw(12) << x;
The screen outputs
_0.166666672
All commands can be written in one line:
cout << fixed << setprecision(9) << setw(12) << x;