What are the signs of crosses initialization?

后端 未结 4 1120
谎友^
谎友^ 2020-11-28 03:28

Consider the following code:

#include 
using namespace std;

int main()
{
    int x, y, i;
    cin >> x >> y >> i;
    swit         


        
4条回答
  •  暖寄归人
    2020-11-28 04:03

    You should put the contents of the case in brackets to give it scope, that way you can declare local variables inside it:

    switch(i) {
        case 1:
            {
                // int r = x + y; -- OK
                int r = 1; // Failed to Compile
                cout << r;
            }
            break;
        case 2:
            ...
            break;
    };
    

提交回复
热议问题