[C ++]与switch()语句有关 [英] [C++] issues with switch() statement

查看:95
本文介绍了[C ++]与switch()语句有关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 switch()创建一个菜单。但是,编译器不断向我抛出错误,如'const char *'无效转换为'char'[-fpermissive] 。以下是代码:

I am trying to create a menu using switch(). However, the compiler keeps throwing me errors like invalid conversion of 'const char*' to 'char' [-fpermissive]. Here is the code:

void info::get_factorio_version(string* factorio_version){
    bool loop = true;
    while(loop){
    cout << "What version of Factorio is this mod for?" << endl;
    cout << "   1 - 0.14.xx" << endl;
    cout << "   2 - 0.13.xx" << endl;
    cout << "   3 - 0.12.xx or earlier" << endl;
    cout << "Your selection: ";
    int select; cin >> select;
    switch(select){
        case '1': {
            *factorio_version = "0.14";
            cout << "\n\n";
            loop = false;
            } break;
        case '2': {
            *factorio_version = "0.13";
            cout << "\n\n";
            loop = false;
            } break;
        case '3': {
            *factorio_version = "NULL";
            cout << "\n\n";
            loop = false;
            } break;
        default: {
            cout << "\n\nInvalid response.\n\n";
            } break;}}}



我的代码是否有问题?



我尝试过:



我尝试过使用:


Is there some issue with my code?

What I have tried:

I have tried using:

string selectSTR; cin >> stringSTR;
int selectINT = atoi(selectSTR);



以及:


As well as:

string selectSTR; cin >> stringSTR;
char selectCHAR = selectSTR.c_str();

推荐答案

您已声明选择 int ,但您的案例语句使用字符常量。它们应该是这样的:

You have declared select as int, but your case statements are using character constants. they should be like:
switch(select){
    case 1:
        *factorio_version = "0.14";



此外,您不需要块分隔符( {} )围绕您的案例代码。


Also you do not need block separators ({ and }) surrounding your case code.


这篇关于[C ++]与switch()语句有关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆