Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - How to use cout function to realize format output in c++
How to use cout function to realize format output in c++
# include & ltiostream & gt

# include & ltiomanip & gt// Don't forget to include this header file.

Use namespace std

int main()

{

int a;

Cout < < "Enter a:";

CIN & gt; & gta;

cout & lt& lt" dec:" & lt; & ltdec & lt& lta & lt& ltendl? //Output integers in decimal form

cout & lt& lt" hex:" & lt; & lt hexadecimal & lt& lta<& ltendl? //Output the integer a in hexadecimal form.

cout & lt& lt" oct:" & lt; & ltset base(8)& lt; & lta & lt& ltendl? //Output the integer a in octal form?

Char *pt= "China"; ? //pt points to the string "China"?

cout & lt& ltsetw( 10)& lt; & ltpt & lt& ltendl? //Specify the domain width as, and output the string?

cout & lt& ltset fill(' * ')& lt; & ltsetw( 10)& lt; & ltpt & lt& ltendl? //Specify the field width, output the string, and fill the spaces with' *'?

Bipi = 22.0/7.0; ? //Calculate the value of pi?

//Output in exponential form, with 8 decimal places?

cout & lt& ltsetios flags(IOs::scientific)& lt; < set accuracy (8); ?

cout & lt& lt" pi = " & lt& ltpi & lt& ltendl? //output pi value?

cout & lt& lt" pi = " & lt& ltsetprecision(4)& ltpi & lt& ltendl? //Change to decimal places?

cout & lt& lt" pi = " & lt& ltsetios flags(IOs::fixed)& lt; & ltpi & lt& ltendl? //Output in decimal form instead?

Returns 0;

}

Extended data

# Contains? & ltcstdio & gt//? In C and some ancient C++, it is STDIO. H. The new standard is to make a standard library.

//? The header file of is different from the user header file, so it is recommended not to use the extension.

//? For the original C library, C should be added in front of the header file name without using the extension.

Class? My outflow

{

Public:

const? My outflow & Operator? & lt& lt? (int? value)const; //Overloading of integer variables

const? My outflow & Operator? & lt& lt? (char*? str)const; //Overloading of string type

};

const? My outflow & My outbound:: operator? & lt& lt(int? Value) constant

{

Printf("%d ",value);

return*? This; //Pay attention to this return. ...

}

const? My outflow & My outbound:: operator? & lt& lt(char*? Str) constant

{

printf("%s ",str);

return*? This; //Again, please pay attention here. ...

}

My outflow? Myout//myout, a global object that can serve us anytime and anywhere.

int? Master ()

{

int? A=2003;

char*? MyStr= "Hello, world!" ;

I'm going out? & lt& lt? myStr? & lt& lt? ”\ n”;

Return? 0;

}

References:

Baidu encyclopedia counting function