PixelFallHD PixelFallHD - 6 months ago 36
C++ Question

C++ cross initiialization error

When compiling the following code it provides me with two errors; "jump to case label" and "crosses initialization of std::ofstream SaveLogin". I am currently using codeblocks with the gnu gcc compiler.

switch(choice) {
case 1:
// login
case 2:
// register
ofstream SaveLogin;
case 3:
// reset password


Thanks for viewing my question-EDIT: my question was flagged as a duplicate of another question but the question specified did not entirely conclude my problem. This post is specifically about my problem although I hope it can hope any other people who are having the same problem


The compiler is correct: C++ does not allow you to skip over a variable declaration in the same scope. goto is similarly restricted.

If you need SaveLogin for all cases then declare it above the switch. If you only need it for case 2 then use scope blocks:

case 2 /*not directly relevant but there's no need for the parentheses*/:
    // register
    ofstream SaveLogin; /*ToDo - more logic here unless it's contained
                         fully within construction and destruction*/
case 3: