C ++ cout

Source: Internet
Author: User
When I did my experiment today, I suddenly found that I would not use cout to output data in hexadecimal notation. So I searched for the data online and found that the following text is exactly what I needed. Therefore, post this article for your own purposes.
At the same time, I am not even familiar with such basic things ......

Source:Http://edept.seu.edu.cn/Extend/CLanguage/courses/JcnrCh01-8-3.htm

1.8.3 input/output format Control


◆ 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;// 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.

◆ 2. Data Interval

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>

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.