https://rentry.org/PPP2_p107b

// Code derived from Stroustrup's PPP2 book
// § 4.4.1.3 Switch technicalities
//  -and beginning on p 107

#include <iostream>

using std::cin;
using std::cout;

// you can label a statement using several case labels
int main()
{
  cout << "Please enter a digit\n";
  char a;
  cin >> a;

  switch (a) {
    case '0':
    case '2':
    case '4':
    case '6':
    case '8': cout << "is even\n"; break;
    case '1':
    case '3':
    case '5':
    case '7':
    case '9': cout << "is odd\n"; break;
    default: cout << "is not a digit\n"; break;
  }
}

build & run:

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

PrevUpNext

Edit
Pub: 29 Jan 2023 17:09 UTC
Edit: 29 Apr 2023 07:06 UTC
Views: 500