相关疑难解决方法(0)

使用C++中的开关评估字符串

我想用开关评估一个字符串但是当我读取用户输入的字符串时会抛出以下错误.

#include<iostream>
using namespace std;

    int main() {
        string a;
        cin>>a;
        switch (string(a)) {
        case "Option 1":
            cout<<"It pressed number 1"<<endl;
            break;
        case "Option 2":
            cout<<"It pressed number 2"<<endl;
            break;
        case "Option 3":
            cout<<"It pressed number 3"<<endl;
            break;
        default:
            cout<<"She put no choice"<<endl;
            break;
        }
        return 0;
    }
Run Code Online (Sandbox Code Playgroud)

错误:从类型'std :: string {aka std :: basic_string}'无效转换为类型'int

c++

47
推荐指数
4
解决办法
21万
查看次数

标签 统计

c++ ×1