#include <iostream>
int main() {
std::cout << "Please input a integer: ";
int t = 0;
std::cin >> t;
switch (t) {
case 0:
int a; a = 0;
//int a = 0;
std::cout << "You intput " << a << '.' << std::endl;
break;
case 1:
a = 1;
std::cout << "You intput " << a << '.' << std::endl;
break;
default:
a = t;
std::cout << "You intput " << a << '.' << std::endl;
break;
}
return 0;
}
The scope of variable “a” is between the braces of the switch statement. The commented line will cause a compiler error(g++):
- variable_initialization_in_switch.cc:14: error: jump to case label
variable_initialization_in_switch.cc:11: error: crosses initialization of ‘int a’
Don’t know what exactly “cross initialization” means by now.