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?