如何从 C++ 中的 UserInput 字符串为 switch case 分配枚举

发布于 2025-01-10 17:12:00 字数 800 浏览 4 评论 0原文

我正在编写这段代码,它接受一个 char userinput 并使用 switch 语句执行相应的命令。但是,由于 C++ switch 语句只能读取整数和枚举,因此我不确定如何合并它。用户输入必须是字符。有什么想法吗?我知道 charInput >> enumInput 不起作用,但我不知道该怎么做。

int main(int argc, char** argv) {
    enum charCommand { i, d, s, n, r, a, m, t, p, l, q };
    charCommand enumInput;

    while (mainLoop) {
        cout << "enter valid input" endl;


        char charInput;
        printf("Enter a command: ");
        cin >> charInput;
        charInput >> enumInput;


        switch (charInput) {
        case i: {
            printf("This is case i");
            exit(0);
        } //case i
        default: {
            cout << "invalid command, try again!\n";
        }
        }
    };
}

I'm writing this code that takes in a char userinput and uses the switch statement do execute the respective command. However because C++ switch statements can only read ints and enums, I'm not sure how to incorporate this. The user input must be a char. any ideas? I know charInput >> enumInput doesn't work but I'm not sure what to do.

int main(int argc, char** argv) {
    enum charCommand { i, d, s, n, r, a, m, t, p, l, q };
    charCommand enumInput;

    while (mainLoop) {
        cout << "enter valid input" endl;


        char charInput;
        printf("Enter a command: ");
        cin >> charInput;
        charInput >> enumInput;


        switch (charInput) {
        case i: {
            printf("This is case i");
            exit(0);
        } //case i
        default: {
            cout << "invalid command, try again!\n";
        }
        }
    };
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心安伴我暖 2025-01-17 17:12:00

实现此目的的一种方法是将输入的实际字符映射到其相应的enum

#include <map>
//...
int main (int argc, char** argv) 
{
    enum charCommand {i,d,s,n,r,a,m,t,p,l,q};
    std::map<char, charCommand> charToEnumMap = {{'i',i}, 
                                                 {'d',d}, 
                                                 {'s',s}}; // Add the rest
    charCommand enumInput;
    while (mainLoop) 
    {
        cout << "enter valid input" endl;
        char charInput;
        printf("Enter a command: ");
        cin >> charInput;
        auto iter = charToEnumMap.find(charInput);
        if ( iter != charToEnumMap.end() ) 
        {
            switch (iter->second) 
            {
                case i : 
                {
                    printf("This is case i");
                    exit(0);
                } //case i
                default: 
                {
                    cout << "invalid command, try again!\n";
                }
            }
        }
    }
}

One way to accomplish this is to map the actual character entered to its corresponding enum:

#include <map>
//...
int main (int argc, char** argv) 
{
    enum charCommand {i,d,s,n,r,a,m,t,p,l,q};
    std::map<char, charCommand> charToEnumMap = {{'i',i}, 
                                                 {'d',d}, 
                                                 {'s',s}}; // Add the rest
    charCommand enumInput;
    while (mainLoop) 
    {
        cout << "enter valid input" endl;
        char charInput;
        printf("Enter a command: ");
        cin >> charInput;
        auto iter = charToEnumMap.find(charInput);
        if ( iter != charToEnumMap.end() ) 
        {
            switch (iter->second) 
            {
                case i : 
                {
                    printf("This is case i");
                    exit(0);
                } //case i
                default: 
                {
                    cout << "invalid command, try again!\n";
                }
            }
        }
    }
}
从来不烧饼 2025-01-17 17:12:00

这里不需要enumswitch 适用于 char,因为 char 可转换为 int。因此,如果我们这样定义一个 char

char c = 'a';

..然后打开它:

switch (c)

这有效,因为 'a' 可以转换为 int ( ASCII 值)。所以这也可以写成:

switch (int(c)) // Same as switch (c)

例子:

#include <iostream>

int main(int argc, char** argv) 
{
    char input; std::cin >> input;

    switch (input)
    {
    case 'a':
        std::cout << 'a' << std::endl;
        break;
    case 'b':
        std::cout << 'b' << std::endl;
        break;
    case 'c':
        std::cout << 'c' << std::endl;
        break;
    default:
        std::cout << "Some other key" << std::endl;
        break;
    }
}

上面的例子是有效的。对于您的情况,我会将您的程序重写为:

#include <iostream>

int main(int argc, char** argv) {

    while (true) {
        std::cout << "enter valid input" << std::endl;

        char charInput;
        printf("Enter a command: ");
        std::cin >> charInput;


        switch (charInput) {
        case 'i':
            printf("This is case i");
            exit(0); //case i
        default:
            std::cout << "invalid command, try again!\n";
            break;
        }
    };
}

另外,请考虑不在代码中使用以下内容:

using namespace std;

..因为它被认为是一种不好的做法。有关这方面的更多信息,请查找 为什么考虑“使用命名空间 std”这是一种不好的做法

There's no need for an enum here. switch works with char because char is convertible to int. So if we define a char as such:

char c = 'a';

..and then switch on it:

switch (c)

This works because 'a' can be converted to int (ASCII value). So this can also be written as:

switch (int(c)) // Same as switch (c)

Example:

#include <iostream>

int main(int argc, char** argv) 
{
    char input; std::cin >> input;

    switch (input)
    {
    case 'a':
        std::cout << 'a' << std::endl;
        break;
    case 'b':
        std::cout << 'b' << std::endl;
        break;
    case 'c':
        std::cout << 'c' << std::endl;
        break;
    default:
        std::cout << "Some other key" << std::endl;
        break;
    }
}

The above example works. For your case, I would rewrite your program as:

#include <iostream>

int main(int argc, char** argv) {

    while (true) {
        std::cout << "enter valid input" << std::endl;

        char charInput;
        printf("Enter a command: ");
        std::cin >> charInput;


        switch (charInput) {
        case 'i':
            printf("This is case i");
            exit(0); //case i
        default:
            std::cout << "invalid command, try again!\n";
            break;
        }
    };
}

Also, consider not using the following in your code:

using namespace std;

..as it's considered as a bad practice. For more info on this, look up why is "using namespace std" considered as a bad practice.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文