I am having some trouble using a switch statement with user input. Can anyone please explain what is going on? I am sorry if this is a noob question as I'm very used to Python and just started learning C++.
#include <iostream>
#include <string>
using namespace std;
#include <cstdlib>
int main()
{
string name;
cout << "Enter a name: ";
cin >> name;
switch (name){
case name == "Seth":
std::cout << "That's my name!";
return 0;
break;
case name == "seth":
std::cout << "Wow, you couldnt even put correct capitalization on my name...\n";
std::cout << "LEARN YOUR PRONOUNS AND GO BACK TO SCHOOL!";
return 0;
break;
case name == "SETH":
std::cout << "Ok ok you spelled my name right but make sure you turn off caps lock please";
return 0;
break;
default:
std::cout << "Come on get my name RIGHT!!!\n";
std::cout << "But you entered " << name;
}
return 0;
}
casestatements, though it doesn't matter because you cannotswitchonstd::stringanywayif-else-ifladder or a lookup table.casespecifies values which are matched, not a condition. So conceptually, in your sample,case "Seth":. However, as UnholySheep mentioned, you can't use a String in a case, only integer types (char,, short, int, unsigned, etc).