In actual program development, the C ++ programming language has many important application skills that need to be mastered to facilitate application development and improve programming efficiency. Here we will introduce the application skills of C ++ standard input and output for your convenience.
◆ 1. Number Base
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; // The input value is a hexadecimal number.
- Cin> k; // The input value is still hexadecimal.
- Cin> dec> l; // The input is in decimal format.
- Cout <"hex:" <"I ="
- Cout <"dec:" <"j =" <dec <j <'\ t' <"k =" <k <endl;
- Cout <"oct:" <"l =" <oct <l;
- Cout <dec <endl; // restores the decimal output state.
Execution result ]:
1) Output prompt: Input I (oct), j (hex), k (hex), l (dec ):
2) Enter 032 0x3f 0xa0 17 on the keyboard. <CR>
3) the output result is:
- hex:i=1a
- dec:j=63 k=160
- oct:l=21
C ++ standard input/output operations:
- C ++ standard Extended Application Skills
- Analysis of C ++ profile application skills
- Overview of C ++ Bost Library
- Basic concepts of C ++ memory usage Mechanism
- Introduction to several different C ++ inheritance Methods
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.
◆ 2. Data Interval
Common setting methods: output space characters or carriage return line breaks.
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
Note:
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 () can only limit one data item following it, and return to the default output mode after output.
To use setw (), you must add the following statement at the beginning of the program: # include <iomanip>
The above is an introduction to the C ++ standard input and output.