https://rentry.org/PPP2_p107a

// 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;

// case labels must be constants
int main()
{
  // define alternatives:
  int            y = 'y';  // this is going to cause trouble
  constexpr char n = 'n';  //
  constexpr char m = '?';

  cout << "Do you like fish?\n";
  char a;
  cin >> a;

  switch (a) {
    case n:
      // . . .
      break;
    case y:  // error: variable in case label
      // . . .
      break;
    case m:
      // . . .
      break;
    case 'n':  // error: duplicate case label (n’s value is ‘n’)
      // . . .
      break;
    default:
      // . . .
      break;
  }
}

build & run:

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

PrevUpNext

Edit
Pub: 29 Jan 2023 13:13 UTC
Edit: 29 Apr 2023 07:04 UTC
Views: 702