0

I'm learning C++ and experimenting with switches.

For some reason my expression looks like it is being matched with cases it does not actually match.

For example see the snippet below. Here, if the user provides input [A], it still triggers the cases that should match input [B] and [C].

#include <iostream>

int main() {
  char choice;

  while (choice != 'A' && choice != 'B' && choice != 'C') {
    std::cout << "\nInput either A, B, or C\n";
    std::cin >> choice;
  }

  switch (choice) {

    case 'A':
      std::cout << "\nCase A\n";
    case 'B':
      std::cout << "\nCase B\n";
    case 'C':
      std::cout << "\nCase C\n";      
      
  }
}

How have I done this? What do I not understand?

Solebay Sharp
  • 487
  • 4
  • 21
  • 2
    Execution falls through, it doesn't leave the switch after the `std::cout`. Common practice is to add a `break;` before each `case` (except the first). – François Andrieux Apr 28 '22 at 13:30

0 Answers0