18

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?

2 Answers 2

20

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.

2
  • str can not be used after the close brace of the scope. What is the sense to enter a string and it can not longer be used?
    – Klaus
    Commented May 10, 2020 at 7:17
  • 1
    This is a small example. It is not used in the original code as well. But one can do all the processing needed for choice == 2 inside the switch statement: calling functions, pushing str to vectors, etc. If you need to access str strictly after the switch statement, it's a smell: there are no guarantees that str will have a reasonable value after the switch in general.
    – yeputons
    Commented May 10, 2020 at 7:36
12

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
3
  • What is the sense of having the string only inside the scope? After the close brace, it can not longer be used.
    – Klaus
    Commented May 10, 2020 at 7:18
  • 3
    I assumed that was the intention, otherwise why declare it inside the switch statement?
    – john
    Commented May 10, 2020 at 7:28
  • 3
    I guess it's worth to add a hint that variables should be declared in its most inner scope which just spans the intended life-time at best. In certain cases, it's even useful to add an extra scope for a well-defined life-time of a variable. (E.g. it's recommendable for lock guardians or other occurrences of RAII.) Commented May 10, 2020 at 7:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.