C++/Data Type/int output

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Decimal in decimal

  
#include <iostream>
using namespace std;
int main()
{
  int x = 100;
  double f = 98.6;
  double f2 = 123456.0;
  double f3 = 1234567.0;
  cout.setf(ios::dec, ios::basefield);
  cout << "x in decimal: " << x << "\n\n";
  return 0;
}


Decimal in hexadecimal

  
#include <iostream>
using namespace std;
int main()
{
  int x = 100;
  double f = 98.6;
  double f2 = 123456.0;
  double f3 = 1234567.0;
  cout.setf(ios::hex, ios::basefield);
  cout << "x in hexadecimal: " << x << endl;

  return 0;
}


Decimal in octal

  
#include <iostream>
using namespace std;
int main()
{
  int x = 100;
  double f = 98.6;
  double f2 = 123456.0;
  double f3 = 1234567.0;
  cout.setf(ios::oct, ios::basefield);
  cout << "x in octal: " << x << endl;
  return 0;
}


Display decimal value as hexadecimal value

  
#include <iostream>
using namespace std; 
int main(void){
   cout << "Decimal value " << dec << 0xFF;
   cout << "\nOctal value " << oct << 10;
   cout << "\nHexadecimal value " << hex << 255;
}


Display decimal value as Octal value

  
#include <iostream>
using namespace std; 
int main(void){
   cout << "Decimal value " << dec << 0xFF;
   cout << "\nOctal value " << oct << 10;
   cout << "\nHexadecimal value " << hex << 255;
}


Left justification

  
#include <iostream>
#include <iomanip>
using namespace std; 
int main(void){
   int i;
   
   cout << "\nLeft justification\n";
   for (i = 0; i < 3; i++){
       cout.width(5);
       cout << setiosflags(ios::left) << i;
   }
}


Making I/O in Octal Format

   
#include <iomanip>
#include <iostream>
using namespace std; 
int main( )
{
   cout << "Enter an octal number: ";
   int num;
   cin >> oct >> num;
   // Display number in decimal and octal
   cout  << "Decimal: " << num << "\tOctal: " << oct << num
         << "\tOctal with base: " << showbase << num << endl;
}


Output integer with different base: 8, 10, 16

  
#include <iostream>
#include <iomanip>
using namespace std; 
int main(void) {
   cout << setbase(8) << 255 << "\n";
   cout << setbase(10) << 255 << "\n";
   cout << setbase(16) << 255 << "\n";
}


Printing an integer with internal spacing and forcing the plus sign.

  
#include <iostream>
#include <iomanip>
using std::cout;
using std::endl;
using std::ios;
using std::setiosflags;
using std::setw;
int main()
{
   cout << setiosflags( ios::internal | ios::showpos )
        << setw( 10 ) << 123 << endl;
   return 0;
}


Right justification

  
#include <iostream>
#include <iomanip>
using namespace std; 
int main(void){
   int i;
   cout << "Right justification\n";
   for (i = 0; i < 3; i++){
       cout.width(5);
       cout << setiosflags(ios::right) << i;
   }
}


Set showpos flag for decimal

  
#include <iostream>
using namespace std;
int main()
{
  int x = 100;
  double f = 98.6;
  double f2 = 123456.0;
  double f3 = 1234567.0;
  // Set the showpos flag.
  cout.setf(ios::showpos);
  cout << "Setting showpos flag.\n";
  cout << "x in decimal after setting showpos: " << x << endl;
  return 0;
}


Set width of output

  
#include <iostream>
using namespace std; 
int main(void){
   int i;
   for (i = 0; i < 3; i++){
     cout.width(5);
     cout << i << "\n";
    }
}


Use .(dot) as place holder

  
#include <iostream>
using namespace std; 
int main(void){
   int i;
   for (i = 0; i < 3; i++){
     cout.fill(".");
     cout.width(5 + i);
     cout << i << "\n";
   }
}


Using hex, oct, dec and setbase stream manipulators.

  
#include <iostream>
#include <iomanip>
using std::cout;
using std::cin;
using std::endl;
using std::hex;
using std::dec;
using std::oct;
using std::setbase;
int main()
{
   int n;
   cout << "Enter a decimal number: ";
   cin >> n;
   cout << n << " in hexadecimal is: " 
        << hex << n << "\n"
        << dec << n << " in octal is: " 
        << oct << n << "\n"
        << setbase( 10 ) << n << " in decimal is: " 
        << n << endl;
   return 0;
}