小编Ant*_*ony的帖子

你如何接受一个 switch case 的多个值?

如何在 C++ 中为单个案例接受多个值?我知道您可以使用case 1..2其他一些语言为一个案例(例如)设置一系列值,但它似乎不适用于 Xcode 上的 C++。

int main() {
    int input;
    cin >> input;
    switch (input) {
        case 1:
            cout << "option 1 \n";
            break;
        case 2..3: //This is where the error occurs
            cout << "option 2 and 3 \n";
            break;
        
        default:
            break;
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

该程序显示错误,指出“浮动常量上的后缀无效'.3'”范围所在。

c++ case switch-statement

1
推荐指数
2
解决办法
66
查看次数

如何找到字符数组的大小?

我应该如何在 C++ 中查找字符数组的长度?我已经尝试了两种方法,但它们都导致数组中的字符数错误。到目前为止,我已经使用了strlensizeof运营商,但无济于事。

void countOccurences(char *str, string word)
{
    char *p;
    string t = "true";
    string f = "false";

    vector<string> a;

    p = strtok(str, " ");
    while (p != NULL)
    {
        a.push_back(p);
        p = strtok(NULL, " ");
    }

    int c = 0;
    for (int i = 0; i < a.size(); i++)
    {
        if (word == a[i])
        {
            c++;
        }
    }

    int length = sizeof(str); //This is where I'm having the problem
    string result;
    cout << length …
Run Code Online (Sandbox Code Playgroud)

c++ arrays sizeof char

-1
推荐指数
1
解决办法
132
查看次数

标签 统计

c++ ×2

arrays ×1

case ×1

char ×1

sizeof ×1

switch-statement ×1