Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jump bypasses variable initialization in switch statement

I need an std:: vector<char> or an std:: string in my switch case for some purpose. So, I wrote the following dummy code to see if it works:

#include <iostream>
#include <string>
int main() {
    int choice = 0;

    do {
        std:: cout << "Enter Choice" << std::endl;
        std:: cin >> choice;

        switch(choice) {
            case 1:
                std::cout << "Hi";
                break;

            case 2:
                std::string str;
                std::cin >> str;
                break;

            case 3: //Compilation error, Cannot jump from switch statement to this case label
                std::cout << "World" << std:: endl;
                break;

            default:
                std:: cout << "Whatever" << std:: endl;
        }

    } while(choice != 5);
    return 0;
}

Ok, I somewhat got it that str is an object of std:: string type. So, I am trying to jump through this variable initialization.

Then why a definition of C style string does not cause compilation error:

#include <iostream>
#include <string>
int main() {
    int choice = 0;

    do {
        std:: cout << "Enter Choice" << std::endl;
        std:: cin >> choice;

        switch(choice) {
            case 1:
                std::cout << "Hi";
                break;

            case 2:
                char str[6];
                std::cin >> str;
                break;

            case 3:
                std::cout << "World" << std:: endl;
                break;

            default:
                std:: cout << "Whatever" << std:: endl;
        }

    } while(choice != 5);
    return 0;
}

How can I make the first code to work?

like image 237
Arun Suryan Avatar asked May 10 '20 06:05

Arun Suryan


People also ask

Can you initialize variables in a switch statement?

The variable can be declared, but it cannot be initialized.

How do you declare variables in the switch block without an error?

You can still declare variables in switch statements, you just have to put curly brackets around the code after the case label.

Which variable is not allowed in switch statement?

The switch statement doesn't accept arguments of type long, float, double,boolean or any object besides String.

Can we use variables in switch case in C?

Do not declare variables inside a switch statement before the first case label. According to the C Standard, 6.8.


2 Answers

char str[6]; is default-initialized. In case of C arrays of simple values with automatic storage duration ("allocated on stack") it means "no initialization at all", so I guess it's not an error.

If you, however, initialize the array like char str[6] = {}, it will yield an error.

I suggest you put extra curly braces so str is in its own scope and is not visible in further case statements:

#include <iostream>
#include <string>
int main() {
    int choice = 0;

    do {
        std:: cout << "Enter Choice" << std::endl;
        std:: cin >> choice;

        switch(choice) {
            case 1:
                std::cout << "Hi";
                break;

            case 2: {  // Changed here
                std::string str;
                std::cin >> str;
                break;
            }
            case 3: // `str` is not available here, no compilation error
                std::cout << "World" << std:: endl;
                break;

            default:
                std:: cout << "Whatever" << std:: endl;
        }

    } while(choice != 5);
    return 0;
}

Where to put brackets is a styling preference.

like image 191
yeputons Avatar answered Oct 17 '22 15:10

yeputons


Just create a new block for the variable with an extra pair of braces

        case 2:
        { // <-- start new block for str
            std::string str;
            std::cin >> str;
            break;
        } // <-- end of block, str will be destroyed here
like image 28
john Avatar answered Oct 17 '22 15:10

john