Default Base:
By default, data is output in decimal format. If the input and output are in octal or hexadecimal format, the corresponding data format must be specified in CIN or cout. OCT is octal, Hex is hexadecimal, and DEC is decimal.
Example:
Int I, J, K, L;
Cout <"input I (OCT), J (HEX), K (HEX), L (DEC):" <Endl;
Cin>Oct> I;// The input value is octal.
Cin>HEX> J;// Enter the hexadecimal number.
Cin> K;// The input value is still hexadecimal.
Cin>Dec> L;// The input value is in decimal format.
Cout <"HEX:" <"I =" <HEX<I <Endl;
Cout <"dec:" <"J =" <Dec<J <<' \ t' <"k =" <k <Endl;
Cout <"Oct:" <"L =" <Oct<L;
Cout <Dec<Endl;// Restore the decimal output state
[Execution result]:
(1) Output prompt:Input I (OCT), J (HEX), K (HEX), L (DEC ):
(2) input from the keyboard:032 0x3f 0xa0 17 <CR>
(3) the output result is:
HEX: I = 1a
Dec: J = 63 K = 160.
Oct: L = 21
Notes:
When using a header file <iostream> without. H, you must specify the number system in CIN. Otherwise, the 0 and 0x signs starting with octal and hexadecimal numbers are not recognized during keyboard input. The 0 and 0x flags can be omitted after being specified.
The hexadecimal control is only applicable to Integer Variables and not to real and complex variables.
The format, number, and type of the input data must correspond to the variables in CIN one by one. Otherwise, the input data is incorrect and the correct input of other data is affected.
After specifying the number system in CIN or cout, the number system will remain valid until another number system is specified.
Common settings: Outputs a space character or a carriage return line break.
Specify the data output width: Use the SETW () function provided by C ++ to specify the width of the output data item. A positive integer value is usually given in the brackets of SETW (), which is used to limit the output width of a data item following it. For example, SETW (8) indicates that the output of the data item following it occupies the width of 8 characters.
Example:
Int I = 2, j = 3;
Float x = 2.6, y = 1.8;
Cout <SETW (6) <I <SETW (10) <j <Endl;
Cout <SETW (10) <I * j <Endl;
Cout <SETW (8) <x <SETW (8) <Y <Endl;
The output result is:
2 3
6
2.6 1.8
Description:
If the actual width of the data is less than the specified width, leave it blank on the left in the right-aligned manner. If the actual width of the data is greater than the specified width, the output is based on the actual width, that is, the specified width is invalid.
SETW ()Only one data item following it can be specified. The default output mode is returned after the output.
UseSETW ()Must be inProgramAdd another sentence at the beginning:# Include <iomanip>