Output C ++ data types and formats

Output format control C ++ 

1. two decimal places

setiosflags(ios::fixed)<<setprecision(2)<<a<<std::endl;

 

 1 #include<iostream>
 2 #include<iomanip>
 3 using namespace std;
 4 int main()
 5 {
 6     float a[5],sum=0;
 7     for(int i=0;i<5;i++)
 8     {
 9         std::cin>>a[i];
10         sum=sum+a[i];
11     }
12     std::cout<<setiosflags(ios::fixed)<<setprecision(2)<<sum/5<<std::endl;
13 }

 2. The left-justified, right-justified
setiosflag (ios :: left), setiosflagdefault right alignment, global settings

 1 #include <iostream>
 2 #include <iomanip>
 3 using namespace std;
 4 int main()
 5 {
 6     int a;
 7     cin>>a;
 8     cout<<setw(10)<<setiosflags(ios::right)<<a<<endl;
 9     return 0;
10

 3. O iostream library, a program using std :: cout and std :: endl, instead of cin and cout is because the prefix is defined in the std :: cout and show endl namespace (namespace) std in. Using namespace programmers can avoid the inadvertent use the same name as the name of the library, as defined in the sky in the conflict.

Plastic Output

Decimal, octal, hexadecimal

 1 #include <iostream>
 2 //#include <cstdio>
 3 using namespace std;
 4 int main()
 5 {
 6     int a;
 7     cin>>oct>>a ;//octonary八进制;
 8     cout<<oct<<a;
 9     //equal;
10     printf(" %o",a);
11     cin>>hex>>a;//hexadecimal十六进制; 
12     cout<<hex<<a;
13     scanf("%x",&A);
 14      the printf ( " % X " , A);
 15      COUT << On Dec; // default output
 16      return  0 ;

 

 

 

Guess you like

Origin www.cnblogs.com/blogs-192-168-9-222/p/cc.html