1

I have a question, It's in not so much a problem, rather curiosity. I have the following code, and when trying to compile I get an error saying:

transfer of control bypasses initialization of: -- variable "var_string"

So I comment it out and then I'm good to go. Question is... Why can a I declare an int variable in between the switch statement and the first case, but not a string variable?

#include <string>
#include <iostream> 

int main(){
    int decider;
    std::cout << "Enter a number: " << std::endl;
    std::cin >> decider;
    switch(decider){
        std::string var_string;
        int var_int;
        case 1:{
            std::cout << "Case N.1" << std::endl;
        }
        break;
        case 2:{
            std::cout << "Case N.2" << std::endl;
        }
        break;
        default:{
            std::cout << "Not an option" << std::endl;
        }
        break;
    }
    return 0;
}

BTW, I know I can simply declare it outside of the switch, I just would like to know

G25
  • 49
  • 4

0 Answers0