First of all, I know breaks fix this issue, but I just want to make sure I understand the default behavior of a switch in C++.
I have the following C++ code that isn't working the way I think it would coming from other languages:
#include <iostream>
using namespace std;
int main(){
int n;
cin >> n;
switch(n) {
case 1: cout << "one" << endl;
case 2: cout << "two" << endl;
case 3: cout << "three" << endl;
case 4: cout << "four" << endl;
case 5: cout << "five" << endl;
case 6: cout << "six" << endl;
case 7: cout << "seven" << endl;
case 8: cout << "eight" << endl;
case 9: cout << "nine" << endl;
default: cout << "Greater than 9" << endl;
}
// your code goes here
return 0;
}
Which outputs:
$ ./a.out
1
one
two
three
four
five
six
seven
eight
nine
Greater than 9
Why are the other cases being hit?
Why is the default behavior different from other languages?