https://rentry.org/PPP2_p106

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

#include <iostream>
#include <string>

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

// you can switch only on integers, etc.
int main()
{
  cout << "Do you like fish?\n";
  string s;
  cin >> s;

  switch (s) {  // error: the value must be of integer, char, or enum type
    case "no":
      // . . .
      break;
    case "yes":
      // . . .
      break;
  }
}

build & run:

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

PrevUpNext

Edit
Pub: 28 Jan 2023 16:26 UTC
Edit: 29 Apr 2023 07:03 UTC
Views: 630