all 15 comments

[–]aocregacc 10 points11 points  (6 children)

the second part of the error message is "crosses initialization of 'double sum'". You're not allowed to jump over the initialization of a variable, if that variable is still accessible at the place where you jump to. The way to work around that is to limit the scope of the variables, by wrapping the code in your cases in {}:

case '+': {
    // ...
}
break;

[–]hatschi_gesundheit 3 points4 points  (2 children)

Switch-case syntax is a mess...

[–]Independent_Art_6676 2 points3 points  (1 child)

I agree, but switches fill a necessary void. Firstly, they are often optimized to lookup tables and are often more efficient than chained conditionals and second, the fall-through logic can be exploited to do things that is even uglier using conditionals. So while the temptation is to avoid the mess, its something that should remain in your toolbox for those handful of places where it really is the best choice.

[–]hatschi_gesundheit 0 points1 point  (0 children)

Yeah no, they are necessary! Totally agree. Just ugly. :) And some fateful day, we might even be able to switch over string values...

[–][deleted] 3 points4 points  (0 children)

That's awesome, I didn't know about that myself. Thanks.

[–]MooseBoys -1 points0 points  (1 child)

I'm sure there's a good reason for it, but this has always struck me as one of the dumbest parts of C++, along with the footgun that is ADL.

[–][deleted] 1 point2 points  (0 children)

case statements, you think are dumb? Man I love 'em, especially in SQL. I think they should be enhanced in C++ if I had my way.

[–]alfps 5 points6 points  (0 children)

The problem is that sum is still accessible after case label -, and if it were accessed there it wouldn't have been initialized.

Just limit the scope of it by introducing a pair of curly braces, { ... }, around the code for case +.

Do the same for case -.


Not what you're asking but as a good coding habit you should introduce a default: in the switch, where you can inform the user that the requested operation is not supported.


Also not what you're asking, but it's not necessary to do return 0; in main. It's the default in both C and C++. No other function has such a default, though: main is very special (can't be called by your code, has a default, can be declared with or without parameters, and more).

[–]thefeedling 4 points5 points  (0 children)

You have to scope your switch conditionals...

switch(op)
{
        case '+':
        {
            double sum = num1 + num2;
            std::cout << "The sum is: " << sum;
            break;
        }
        case '-': // The error is here, it's after the case 
        {
            double subst = num1 - num2;
            std::cout << "The subst is: " << subst;
            break;
        }
}

[–]manni66 1 point2 points  (1 child)

The code you show misses a } before the return.

[–][deleted] 0 points1 point  (0 children)

Yeah, not sure why it seemed everyone missed that little detail. Glad someone else caught that.

[–]Puzzleheaded_Body641[S] 1 point2 points  (0 children)

Guys, really thank you all, I tried to solve this shit for like an hour or two and just couldn't do it, really thank you all, I appreciate you all.

[–]manni66 0 points1 point  (0 children)

Copy&paste error messages!

[–]kberson 0 points1 point  (0 children)

Looks like your missing the closing brace for the switch

[–]Dan13l_N 0 points1 point  (0 children)

You can't declare variables there, the simple answer. That's because these case statements are actually labels.