简体   繁体   中英

switch statement and user input in c++

I am having some trouble using a switch statement with c++ and user input can anyone please explain what is going on! i am sorry if this is a noob question as im 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;
}

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;
    }
}
#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;
}

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM