【问题标题】:Using Switch with enum in C++在 C++ 中使用带枚举的 Switch
【发布时间】:2019-02-14 03:21:13
【问题描述】:

对于以下代码,我想输入日期并获取 cout 值。 目前,如果我输入 0、1、2、3,它会正确给出 cout 值 exp结果:C++程序要求一天,输出一周中的一天的标签。请指教如何解决。

#include <iostream>

using namespace std;

int main()
{

enum days { Sun, Mon, Tue, Wed, Thu,Fri,Sat };
int day;
cout << " Enter a day ";
cin >> day;

switch (day)
{
case 0:
    cout << "Weekend" << endl;
    break;

case 1:
    cout << "Start of work week " << endl;
    break;

case 2:
    cout << "Midweek" << endl;
    break;

case 3:
    cout << "Midweek" << endl;
    break;

case 4:
    cout << "Midweek" << endl;
    break;

case 5:
    cout << "End of work  week" << endl;
    break;

case 6:
    cout << "Weekend" << endl;
    break;

default:
   cout << "Invalid day of the week" << endl;
    return 0;

}
}

【问题讨论】:

  • 你应该使用switch (day) case Sun: case Mon;, case Tue:,不是吗?
  • 我尝试使用 case Mon;, case Tue: 但是它给我的 case Mon -Fri as weeknd 抛出错误,它对 sat 和 sun 有好处。我不知道为什么它显示 weeknd 为 mon -fri ?有什么帮助吗?
  • 无法为您提供任何帮助,因为从我坐的地方我看不到您尝试了什么,也看不到 throwing me error 是什么意思.
  • 我在第一条评论中告诉过你如何解决它。我不明白你的困难。在您的switch 中,将0 替换为Sun,将1 替换为Mon,将2 替换为Tue,等等。它不会抛出任何东西,也不会破坏任何东西。

标签: c++


【解决方案1】:

请注意:

每个 switch 动作都与一个整数常量的值相关联 表达式(即字符和整数常量的任意组合 计算为一个常量整数值)。

从前面的语句中我们看到可以在switch cases 中使用enum 值,因为enum 值被视为数字。

因此,在您的情况下,不可能让用户输入 string 输入,然后您在 switch case 中检查该输入。在将输入与switch case 一起使用之前,您应该将用户输入(无论它是什么)转换为整数常量表达式,以便您可以在switch case 中使用。
你也可以参考this,我觉得对你有帮助。

【讨论】:

  • 代码为int day; cin &gt;&gt; day;。这会将输入读入int,因此尝试使用string 没有问题。此外,switch(expr) 中的表达式不必是常量表达式。事实上,如果是这样,那么使用switch 语句就毫无意义。你是对的,case 标签需要一个完整的常量表达式,但这不是问题,因为它们都是。
【解决方案2】:

开关用于检查整数常量,因此您可以实际打开您拥有的枚举值,例如:

switch (day) {
    case Mon: case Tue: case Wed: case Thu: case Fri:
        cout << "Weekday"; break;
    case Sun: case Sat:
        cout << "Weekend"; break;
    default:
        cout << "??"; break;
}
cout << '\n';

但是,您不能以您期望的方式输入枚举值,输入单词Tue 并将其神奇地转换为值2。如果你有以下代码:

int x = 42;
std::cin >> x;

而您输入的非数字值,例如Tuex不是您可以使用的任何有用值。正如您在评论中提到的那样,这几乎可以肯定是原因,无论您输入哪一天,它都会告诉您这是周末。textual。他们很有可能全部x 设置为零(星期日),因为它们不能立即被解释为整数。


可以做的是提供一个函数来为你完成转换,就像这个完整的程序中显示的那样:

#include <iostream>
#include <string>
#include <algorithm>

int getDayOfWeek(std::string textDay) {
    // Only use up to three characters, and lower-case.

    std::string day = textDay.substr(0,3);
    std::transform(day.begin(), day.end(), day.begin(), ::tolower);

    // Search through collection until found then return index.

    int dayOfWeek = 0;
    for (std::string chk: {"sun", "mon", "tue", "wed", "thu", "fri", "sat"}) {
        if (day == chk) return dayOfWeek;
        ++dayOfWeek;
    }

    // Not found, return sentinel value.

    return -1;
}

// Test harness to allow you to enter arbitrary lines and
// convert them to day indexes. Hit ENTER on its own to stop.

int main() {
    std::string day;
    std::cout << "Enter day: "; std::getline(std::cin, day);
    while (!day.empty()) {
        std::cout << day << " --> " << getDayOfWeek(day) << '\n';
        std::cout << "Enter day: "; std::getline(std::cin, day);
    }
    return 0;
}

这里的“肉”是getDayOfWeek() 函数,给定一个字符串,它可以告诉你该字符串代表一周中的哪一天(如果有的话)。一旦你拥有它是一个整数,在其上使用switch 语句就很简单了,例如:

std::string getDayClass(std::string day) {
    switch getDayOfWeek(day) {
        case 0: case 6:
            return "weekend";
        case 1: case 2: case 3: case 4: case 5:
            return "weekday";
    }
    return "unknown";
}

请记住,我使用了特定规则将文本日期解码为整数,仅检查前三个字符,并且是小写,

所以WEDDED BLISS 作为输入将看到它认为这是一个星期三。显然,您可以根据情况需要使规则更具(或更少)限制性。

【讨论】:

    【解决方案3】:

    你想要这样的东西:

    int main()
    {
        string day;
        cout << " Enter a day ";
        cin >> day;
        if (day == "Sun")
        {
            cout << "Weekend" << endl;
        }
        else if (day == "Mon")
        {
            cout << "Start of work week " << endl;
        }
        ...
        return 0;
    }
    

    【讨论】:

    • 即使我替换了参考代码,它仍然让我在周一至周五度过周末
    • 我想我现在明白你想要什么 - 更新了答案。
    • 我知道 if else 的代码,但我只需要对当前代码使用使用枚举的 switch。
    • 您不能使用switch 进行字符串输入。它仅适用于整数值。
    • 是的,我不能只使用 char、int、enum 和 boolean。你能用我当前的代码为我修复吗
    猜你喜欢
    • 2021-12-17
    • 2011-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-10
    • 2011-03-02
    • 1970-01-01
    相关资源
    最近更新 更多