https://rentry.org/PPP2_p383

// Code derived from Stroustrup's PPP2 book
// § 11.2.2 Integer input
//  -and beginning on p 383

#include <iostream>

using namespace std;

int main()
{
  // example input:  1 2 3 4 ctrl+z
  cout << "demo integer inputs (input some integers):\n";

  int a;
  int b;
  int c;
  int d;
  cin >> a >> hex >> b >> oct >> c >> d;
  cout << a << '\t' << b << '\t' << c << '\t' << d << '\n';

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

  cin.unsetf(ios::dec);  // don’t assume decimal (so that 0x can mean hex)
  cin.unsetf(ios::oct);  // don’t assume octal (so that 12 can mean twelve)
  cin.unsetf(ios::hex);  // don’t assume hexadecimal (so 12 can mean twelve)

  cin >> a >> b >> c >> d;
  cout << a << '\t' << b << '\t' << c << '\t' << d << '\n';
}

build & run:

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

PrevUpNext

Edit
Pub: 09 Apr 2023 04:59 UTC
Edit: 03 May 2023 10:23 UTC
Views: 550