Analysis of C ++ standard input/output application skills

Source: Internet
Author: User

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:

 
 
  1. Int I, j, k, l;
  2. Cout <"Input I (oct), j (hex), k (hex), l (dec):" <endl;
  3. Cin> oct> I; // The input value is octal.
  4. Cin> hex> j; // The input value is a hexadecimal number.
  5. Cin> k; // The input value is still hexadecimal.
  6. Cin> dec> l; // The input is in decimal format.
  7. Cout <"hex:" <"I ="
  8. Cout <"dec:" <"j =" <dec <j <'\ t' <"k =" <k <endl;
  9. Cout <"oct:" <"l =" <oct <l;
  10. 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:

 
 
  1. hex:i=1a 
  2. dec:j=63 k=160 
  3. 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:

 
 
  1. int i=2, j=3;  
  2. float x=2.6, y=1.8;   
  3. cout<<setw(6)<<i<<setw(10)<<j<<endl;  
  4. cout<<setw(10)<<i*j<<endl;  
  5. cout<<setw(8)<<x<<setw(8)<<y<<endl; 

The output result is:

 
 
  1. 2 3  
  2. 6  
  3. 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.