开关盒的C输入

1 c input switch-statement

如何输入String并在Switch命令中使用它?这是代码,但我得到的开关数量不是整数错误.

#include <stdio.h>

int main(void) {
    float usd2 = 0.9117;
    float yen2 = 0.0073;
    float pound2 = 1.4137;
    float eingabe;
    char whr[] = "";

    printf("Bitte Summe in EURO eingeben: ");
    scanf("%f", &eingabe);
    printf("Die Währungnummer eingeben USD, YEN, POUND: ");
    scanf("%s", &whr);

    switch(whr) {
        case "usd": printf("%.4f EURO es sind dann %.4f USD.", eingabe, (eingabe/usd2));
        break;
        case "yen": printf("%.4f EURO es sind dann %.4f YEN.", eingabe, (eingabe/yen2));
        break;
        case "pound": printf("%.4f EURO es sind dann %.4f POUND.", eingabe, (eingabe/pound2));
        break;
        default: printf("Falsche eingabe.");
        break;
    }


    return 0;
}
Run Code Online (Sandbox Code Playgroud)

Ste*_*ner 6

C中没有办法在switch语句的条件或标签常量表达式中使用"string"(在你的情况下是一个字符数组),因为它们不能转换为整数值.C要求条件和常量表达式是整数类型(例如,参见此在线c ++标准草案):

6.8.4.2 switch语句

1 switch语句的控制表达式应具有整数类型.

...

3每个case标签的表达式应为整数常量表达式...

为了解决这个问题,您可以使用级联if (strcmp(whr,"USD")==0) else if (strcmp(whr, "YEN")==0)...或者您可以引入代表货币的枚举并将用户输入映射到此类枚举.由于if-else大片是直接的,我只是展示了第二种方法.使用枚举的优势在于,您可以在整个程序中轻松使用它们,而无需if-else在代码中的不同位置重复-cascades:

typedef enum  {
UNDEFINED, USD, YEN, POUND
} CurrencyEnum;

struct currencyLabel {
    CurrencyEnum currencyEnum;
    const char *currencyString;
} currencyLabels[] = {
    { USD, "USD" },
    { YEN, "YEN" },
    { POUND, "POUND" }
};

CurrencyEnum getCurrencyByLabel(const char* label) {

    for (int i=0; i< (sizeof(currencyLabels) / sizeof(struct currencyLabel)); i++) {
        if (strcmp(label, currencyLabels[i].currencyString) == 0)
            return currencyLabels[i].currencyEnum;  // could use strcasecmp or stricmp, if available, too.
    }
    return UNDEFINED;
}

int main(void) {
    float usd2 = 0.9117;
    float yen2 = 0.0073;
    float pound2 = 1.4137;
    float eingabe;
    char whr[10] = "";

    printf("Bitte Summe in EURO eingeben: ");
    scanf("%f", &eingabe);
    printf("Die Währungnummer eingeben USD, YEN, POUND: ");
    scanf("%s", whr);
    CurrencyEnum currency = getCurrencyByLabel(whr);

    switch(currency) {
        case USD: printf("%.4f EURO es sind dann %.4f USD.", eingabe, (eingabe/usd2));
            break;
        case YEN: printf("%.4f EURO es sind dann %.4f YEN.", eingabe, (eingabe/yen2));
            break;
        case POUND: printf("%.4f EURO es sind dann %.4f POUND.", eingabe, (eingabe/pound2));
            break;
        default: printf("Falsche eingabe.");
            break;
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

顺便说一句:请注意,定义whrchar whr[] = ""实际保留大小为1的字符串,并使用它scanf产生溢出(和未定义的行为).您可以将其定义为char whr[10]类似的东西.


Jac*_*001 5

正如你在其他更高级的语言中看到的那样,C并没有真正的字符串.相反,C字符串只是字符数组.这意味着像比较这样的复杂操作本身并不能真正处理字符串.

由于它们目前是设计的,因此switch语句仅对单个整数值进行操作.这可以表示为单个char或int,这有时会导致混淆.要做你想做的事,你需要使用函数ifelse if语句strcmp().

if( strcmp(whr, "foo") == 0 ) {
    // do a thing
}
else if( strcmp(whr, "bar") == 0 ) {
    // do a different thing
}
else {
    // default thing to do
}
Run Code Online (Sandbox Code Playgroud)

关于strcmp()功能和if陈述的文档.

编辑:

我演示了一个简单的两个条件陈述.实际上,该else语句是可选的,您可以拥有任意数量的else if语句.

您还需要在程序中包含字符串标题.在您所包含的文件顶部执行此操作stdio.h

#include <string.h>
Run Code Online (Sandbox Code Playgroud)