https://rentry.org/PPP2_p381

// Code derived from Stroustrup's PPP2 book
// § 11.2.1 Integer output
//  -and beginning on p 381

#include <iostream>

using namespace std;

int main()
{
  //---
  cout << '\n';

  cout << 1234 << "\t(decimal)\n"
       << hex << 1234 << "\t(hexadecimal)\n"
       << oct << 1234 << "\t(octal)\n";

  //---
  cout << '\n';

  cout << dec << 1234 << '\t' << hex << 1234 << '\t' << oct << 1234 << '\n';
  cout << 1234 << '\n';  // the octal base is still in effect

  //---
  cout << '\n';

  cout << dec << 1234 << '\t' << hex << 1234 << '\t' << oct << 1234 << '\n';
  cout << showbase;  // show bases
  cout << dec << 1234 << '\t' << hex << 1234 << '\t' << oct << 1234 << '\n';

  //---
  cout << '\n';

  cout << dec << 1234 << '\t' << 0x4d2 << '\t' << 02322 << '\n';
}

build & run:

g++ -std=c++20 -O2 -Wall -pedantic ./ch_11/main_p381.cpp && ./a.out

UpNext

Edit
Pub: 09 Apr 2023 04:56 UTC
Edit: 03 May 2023 10:21 UTC
Views: 402