0

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;
}
6
  • 2
    That's not the correct syntax for case statements, though it doesn't matter because you cannot switch on std::string anyway Commented Nov 17, 2021 at 17:52
  • 2
    For strings, you'll need to use either if-else-if ladder or a lookup table. Commented Nov 17, 2021 at 17:55
  • 1
    In C++, case specifies 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). Commented Nov 17, 2021 at 18:00
  • You also don't need a return 0; after every case. That can just be outside all of them. Commented Nov 17, 2021 at 18:06
  • C++. Python. Different languages these are. Different languages with different ways. These new ways, learn you must. Commented Nov 17, 2021 at 18:15

3 Answers 3

3

According to the C++ 17 Standard (9.4.2 The switch statement)

2 The condition shall be of integral type, enumeration type, or class type. If of class type, the condition is contextually implicitly converted (Clause 7) to an integral or enumeration type. If the (possibly converted) type is subject to integral promotions (7.6), the condition is converted to the promoted type. Any statement within the switch statement can be labeled with one or more case labels as follows: case constant-expression : where the constant-expression shall be a converted constant expression (8.20) of the adjusted type of the switch condition. No two of the case constants in the same switch shall have the same value after conversion.

The class std::string does not have an implicit conversion operator that converts an object of the type std::string to an integral or enumeration type.

So the expression in this switch statement

switch (name){

is invalid.

Also case labels like this

case name == "seth":

are syntactically incorrect.

You could resolve your problem with the switch statement for example the following way

#include <iostream>
#include <string>
#include <array>
#include <iterator>
#include <algorithm>

int main()
{
    std::array<const char *, 3> names =
    {
        "Seth", "seth", "SETH"
    };

    std::string name;

    std::cout << "Enter a name: ";
    std::cin >> name;

    size_t n = std::find( std::begin( names ), std::end( names ), name ) - 
                          std::begin( names );

    switch (n)
    {
    case 0:
        std::cout << "That's my name!";
        break;

    case 1:
        std::cout << "Wow, you couldnt even put correct capitalization on my name...\n";
        std::cout << "LEARN YOUR PRONOUNS AND GO BACK TO SCHOOL!";
        break;

    case 2:
        std::cout << "Ok ok you spelled my name right but make sure you turn off caps lock please";
        break;

    default:
        std::cout << "Come on get my name RIGHT!!!\n";
        std::cout << "But you entered " << name;
        break;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0
#include <iostream>
#include <string>

int main()
{
    std::string name;
    std::cout << "Enter a name: ";
    std::cin >> name;
    
    if(name == "Seth")
    {
        std::cout << "That's my name!" << std::endl;
    }
    else if(name == "seth")
    {
        std::cout << "Wow, you couldn't even put correct capitalization on my name..." << std::endl;
        std::cout << "LEARN YOUR PRONOUNS AND GO BACK TO SCHOOL!" << std::endl;
    }
    else if(name == "SETH")
    {
        std::cout << "Ok ok you spelled my name right but make sure you turn off caps lock please" << std::endl;
    }
    else
    {
        std::cout << "Come on get my name RIGHT!!!" << std::endl;
        std::cout << "But you entered " << name << std::endl;
    }
    
    return 0;
}

3 Comments

Tactical note: If you convert name to all-lower or all-upper case, you can greatly reduce the number of ifs required.
@user4581301 but user wants to show different output for specific spellings. I assume user is trying to learn about switch case statements.
Agreed. That's why it's a comment and not an answer.
0

As other people have told that you can not do string comparison in switch and provided a solution. But I would like to use enum class which I found more readable and int comparison much more faster than string comparison.

#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
#include <unordered_map>

int main() {
    enum class Spellings { Seth, seth, SETH };

    const std::unordered_map<std::string, Spellings> spellings_map{
        {"Seth", Spellings::Seth},
        {"seth", Spellings::seth},
        {"Seth", Spellings::SETH},
    };

    std::string name;

    std::cout << "Enter a name: ";
    std::cin >> name;

    auto result = spellings_map.find(name);
    if (result == spellings_map.end()) {
        std::cout << "Come on get my name RIGHT!!!\n";
        std::cout << "But you entered " << name;
        return -1;
    }
    switch (result->second) {
        case Spellings::Seth:
            std::cout << "That's my name!";
            break;

        case Spellings::seth:
            std::cout << "Wow, you couldnt even put correct capitalization on "
                         "my name...\n";
            std::cout << "LEARN YOUR PRONOUNS AND GO BACK TO SCHOOL!";
            break;

        case Spellings::SETH:
            std::cout << "Ok ok you spelled my name right but make sure you "
                         "turn off caps lock please";
            break;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.