1

how to input String and use it in the Switch command? Here is the code but I get switch quantity not an integer error.

#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;
}

3 Answers3

6

There is no way in C to use a "string" (an array of characters in your case) in a switch statement's condition or in the label constant expressions as they cannot be converted to an integral value. C requires the condition and the constant expressions to be of integral type (cf., for example, this online c++ standard draft):

6.8.4.2 The switch statement

1 The controlling expression of a switch statement shall have integer type.

...

3 The expression of each case label shall be an integer constant expression ...

To overcome this, you could use cascades of if (strcmp(whr,"USD")==0) else if (strcmp(whr, "YEN")==0)... or you could introduce an enum that represents the currencies and map the user input to such enums. As the if-else-cascades are straight forward, I just show the second approach. Using enums has the advantage that you can use them easily throughout your program without the need of repeating the if-else-cascades at different places in your code:

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;
}

BTW: Note that defining whr as char whr[] = "" actually reserves a string with size 1, and using it in scanf yields an overflow (and undefined behaviour). You can define it as char whr[10] or something like that.

Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58
5

C doesn't really have strings as you see in other, higher languages. Rather, C strings are simply arrays of chars. That means that complex operations like comparison doesn't really work on strings natively.

As they are currently designed, switch statements only operate on a single integer value. This can be represented as either a single char or an int which sometimes causes confusion. To do what you want, you'll need to use if and else if statements with the strcmp() function.

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

Documentation on strcmp() function and if statements.

Edit:

I've demonstrated a simple two condition statement. In reality, the else statement is optional, and you can have as many else if statements as you'd like.

You'll also need to include the string header in your program. Do this at the top of your file where you're including stdio.h

#include <string.h>
Jacobm001
  • 4,431
  • 4
  • 30
  • 51
  • @eXme: as many as you want. I've added an update to address that. – Jacobm001 Aug 08 '17 at 21:13
  • @eXme: you'll have to give more detail than that for me to help you. This *is* how you compare strings in C. – Jacobm001 Aug 08 '17 at 21:18
  • 15 23 G:\data\owncloud\Project\C\Training\währung\1.cpp [Error] 'strcmp' was not declared in this scope –  Aug 08 '17 at 21:19
  • 1
    @eXme: You need to include the string header. – Jacobm001 Aug 08 '17 at 21:20
  • But it compiles if I write if(whr == "USD" ) but no effect at the end it just goes to the else.. –  Aug 08 '17 at 21:20
  • @eXme: I've added an edit that shows how to include the string header. As I've already said in my answer, you can't use `==` with a string. – Jacobm001 Aug 08 '17 at 21:21
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/151440/discussion-between-jacobm001-and-exme). – Jacobm001 Aug 08 '17 at 21:22
0
#include <stdio.h>
#include <string.h>
int main(void) {
    float usd2 = 0.9117;
    float yen2 = 0.0073;
    float pound2 = 1.4137;
    float eingabe;
    char whr[5] = "";

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

    if( strcmp(whr, "USD") == 0 ) {
    printf("%.4f EURO es sind dann %.4f USD.", eingabe, (eingabe/usd2));
    }
    else if( strcmp(whr, "YEN") == 0 ) { // strcmp(1 array, 2 array) wenn die gleich sind gibt der 0 raus, when linke hat weniger char dann negativ und wenn linke größer dann positiv
    printf("%.4f EURO es sind dann %.4f YEN.", eingabe, (eingabe/yen2));
        }
    else if( strcmp(whr, "POUND") == 0 ) {
    printf("%.4f EURO es sind dann %.4f POUND.", eingabe, (eingabe/pound2));
        }
    else {
        printf("Falsche eingabe.");
    }


    return 0;
}