Format output in C + + programming _c language

Source: Internet
Author: User
Tags control characters terminates setf

When the data is exported, for simplicity, the format of the output is often not specified, and the system takes a default format based on the type of data, but sometimes you want the data to be output in the specified format, such as requiring an integer to be output in 16 or octal, and only two decimal digits for the number of outputs. There are two ways to achieve this. One is a method that uses a control character, and the 2nd is a member function that uses the Flow object. Separately described below.
Controlling output formats with a control character

How to use control formats no longer repeat here, just examples
[Example] controls the output format with a control character.

#include <iostream>
#include <iomanip>//do not forget to include this header file
using namespace std;
int main ()
{
  int A;
  cout<< "input A:";
  cin>>a;
  cout<< "Dec:" <<dec<<a<<endl; Output integers in decimal
  cout<< "hex:" <
Input A:34↙ (Enter a value)
dec:34          (decimal form)
hex:22          (16 in)
oct:42          (octal form) (        domain wide)
*****china        (Field width is, white space is filled with ' * ')
pi=3.14285714e+00    (exponential output, 8-bit decimal)
pi=3.1429e+00      (exponential output, 4-bit decimal)
pi=3.143         (decimal form output, precision still)


Controlling output format with a member function of a Stream object

In addition to controlling the output format with a control character, you can control the output format by calling the member function in the Stream object cout to control the output format. Common member functions for controlling the output format see table

The flow member functions SETF and the parameters in the setiosflags brackets denote the format state, which is specified by a format flag. The format flag is defined as an enumeration value in class iOS. So when referencing these format flags, precede with the class name iOS and the domain operator "::". Format flags see the following table

The example uses the Flow control member function to output data.

#include <iostream> using namespace std;     int main () {int a=21 cout.setf (ios::showbase);//display cardinality symbol (0x or) cout<< "Dec:" <<a<<endl;     The default is to output a COUT.UNSETF (iOS::d ec) in decimal form;      Terminates the decimal formatting cout.setf (Ios::hex);     Set the status in hexadecimal output cout<< "hex:" <<a<<endl;     Output a COUT.UNSETF (Ios::hex) in the form of 16;      Terminates the hexadecimal formatting cout.setf (IOS::OCT);     Set the status of octal output cout<< "Oct:" <<a<<endl;
  Output a cout.unseft (ios::oct) in the form of eight;       Char *pt= "the";        PT points to the string "Cout.width" (10);        Specifies that the domain width is cout<<pt<<endl;        Output string Cout.width (10);        Specifies that the domain width is cout.fill (' * ');        Specify white space to ' * ' Fill cout<<pt<<endl;      Output string double pi=22.0/7.0;  Output pi value cout.setf (ios::scientific);          Specifying the output of cout<< "pi=" by scientific notation;        Output "pi=" Cout.width (14);        Specifies that the domain width is cout<<pi<<endl; Output pi value cout.unsetf (ios::scientific);    Termination of the scientific notation State COUT.SETF (ios::fixed); SpecifiedOutput Cout.width (12) with fixed-point form;   Specifies that the domain width is cout.setf (ios::showpos);   Positive output "+" number cout.setf (ios::internal);      The number of characters appears on the left cout.precision (6);        Preserve bit decimal cout<<pi<<endl;
Output PI, note the number of "+" position return 0;
 }

The operating conditions are as follows:

dec:21 (decimal form)
hex:0x15         (16 in, beginning with x)
oct:025         (octal, beginning)        (field width to)
*****china        (field width is, blank to ' * ' Fill)
pi=**3.142857e+00    (exponential output, field width, default bit decimal)
+***3.142857       (decimal form output, precision, leftmost output number character "+ ”)

A few notes on the program:
1 The member function width (n) and the control character SETW (n) are valid only for the first output item thereafter. Such as:

  cout. Width (6);
  cout <<20 <<3.14<<endl;


Output is 203.14

In the output of the first output item 20 o'clock, the field width is 6, so there are 4 spaces in front of 20, and the width (6) does not work at the output 3.14, which is output by the system default field width (output by the actual length of the data). If you require that you output data with a width of n output in the same domain as you specify, you cannot call width (n) only once, but you must call width (N>) once before outputting each item, as in the previous program.

2 The output format status in the table is divided into 5 groups, each of which can only be selected in one (e.g., Dec, Hex, and Oct, which are mutually exclusive). After you set the output format state with the member function SETF and the control character Setiosflags, you should call the member function UNSETF (corresponding to the member function self) or resetiosflags (corresponding to the control character Setiosflags) if you want to change to another state of the same group. , the state of the original setting is terminated first. And then set up other states, you can see this in this program. The program at the beginning although not with the member function self and the control character Setiosflags set with the DEC output format state, but the system by default specified as Dec, so to change to hex or Oct, you should also use the UNSETF function to terminate the original settings. If you delete lines 7th and 10th in the program, although the hex and Oct formats are set with the member function setf in line 8th and 11th, the DEC format is not terminated, so the hex and Oct settings do not work and the system is still exported in decimal form.

Similarly, the call to the UNSETF function in line 8th of the program is indispensable.

3 When the format state is set with the SETF function, it can contain two or more format flags, which are defined as enumeration values in the iOS class, each of which is represented by a binary, so you can use the bit or operator "|" Combine multiple formatting flags. For example, lines 5th and 6th can be replaced with the following line:
COUT.SETF (ios::internal I ios::showpos); Contains two status flags, with "|" Combination

4 You can see: The control of the output format, can be used either by the controller or cout flow of the relevant member functions, the effect is the same. The control character is defined in the header file Iomanip, so you must include the Iomanip header file when you use the control character. The member functions of the cout stream are defined in the header file iostream, so you only need to include the header file iostream, and you do not have to include Iomanip. Many program people feel that using a control character is easy, and you can use a variety of control characters in a cout output statement.

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.