c++ switch string
Switch statements in C++ cannot be used with strings directly. However, you can achieve similar functionality using a map or a series of if-else statements. Here's an example using a map:
#include <iostream>
#include <map>
#include <string>
int main() {
std::map<std::string, int> stringMap;
stringMap["one"] = 1;
stringMap["two"] = 2;
stringMap["three"] = 3;
std::string input = "two";
switch (stringMap[input]) {
case 1:
std::cout << "You chose one";
break;
case 2:
std::cout << "You chose two";
break;
case 3:
std::cout << "You chose three";
break;
default:
std::cout << "Invalid choice";
break;
}
return 0;
}