-1

How to use strings in a switch statement? For now I used the first letter of the string in the switch statement. Here is my code. I want to use the whole string in char a, b, c as the input in switch. How to?

int main()
{
    char input[10];
    int x, y, i;
    int AX;
    char a[] = "ADD";
    char b[] = "PRT AX";
    char c[] = "EXIT";

    for (i = 0; i < 100; i++)
    {
        printf("\nType ADD following the two numbers to be added\n");
        printf("     PRT AX to display the sum\n");
        printf("     EXIT to exit program\n");
        printf("---->");
        scanf("%s", &input);

        switch (input[0])
        {

        case 'A':

            printf("\nEnter two numbers you want to add:\n");
            scanf("%d %d", &x, &y);
            break;
        case 'P':
            printf("Sum: %d\n\n", x + y );
            break;
        case 'E':
            exit(0);
        default:
            i++;
        }
    }

    return 0;
}
Bryan Reyes
  • 111
  • 1
  • 1
  • 8
  • 4
    You can only `switch` over integral values in C. Use multiple `strcmp`s in an `if`, `else if` cascade, grouping the likely cases near the beginning. – 5gon12eder Nov 29 '14 at 13:00
  • 1
    Related: http://stackoverflow.com/questions/4014827/best-way-to-switch-on-a-string-in-c (It's a question that is closed as some people think that it is unclear. Seems very clear to me.) – Guffa Nov 29 '14 at 13:03
  • @Bryan Reyes Do not be hurry. See my answer.:) – Vlad from Moscow Nov 29 '14 at 13:31

2 Answers2

1

You can't. The C11 standard is very clear about what may be allowed in switch statements:

6.8.4.2 The switch statement

Constraints

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 and no two of the case constant expressions in the same switch statement shall have the same value after conversion. ...

Note: characters have integer type in C.

Instead, you want to use strcmp to compare strings. Do not use the comparison operator! strcmp will return 0 if two strings are equal.

1

Try something like

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(void) 
{
    char input[10];
    int x, y, i;

    const char * a[3] = 
    {
        "ADD",
        "PRT AX",
        "EXIT",
    };

    enum CHOICE { ADD, PRT_AX, EXIT, WRONG } choice;

    for ( i = 0; i < 100; i++ )
    {
        size_t n;
        printf( "\nType ADD following the two numbers to be added\n" );
        printf( "     PRT AX to display the sum\n" );
        printf( "     EXIT to exit program\n" );
        printf( "---->" );
        fgets( input, sizeof( input ), stdin );

        n = strlen( input );
        if ( n && input[n-1] == '\n' ) input[n-1] = '\0';

        choice = ADD;
        while ( choice != WRONG && strcmp( a[choice], input ) != 0 )
        {   
            choice = ( enum CHOICE )( choice + 1 );        
        }

        switch ( choice )
        {
        case ADD:
            printf( "\nEnter two numbers you want to add:\n" );
            scanf( "%d %d", &x, &y );
            fgets( input, sizeof( input ), stdin );
            break;
        case PRT_AX:
            printf( "Sum: %d\n\n", x + y );
            break;
        case EXIT:
            puts( "Exiting..." );
            exit(0);
        default:
            i++;
        }
    }
    return 0;
}

Also take into account that you should read wntire line using fgets and then apply sscanf.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Can't display the sum when I typed PART AX. – Bryan Reyes Nov 29 '14 at 13:40
  • 1
    @Bryan Reyes First of all in your code there is PRT AX instead of PART AX. Nevertheless the problem is that you are trying to enter two words while scanf scns only one word, As I wrote in my answer you have to use fgets instead of scanf if you want to use more than one word as input. – Vlad from Moscow Nov 29 '14 at 13:46
  • consider using a n-sized array of const char* instead of char a[3][10], since all arguments are string constants. Less memory use, more safety in case one of a later set of arguments is longer than 10 chars. ex: const char *a[3] = {"hello", "world", "bye"}; – midor Nov 29 '14 at 14:01
  • @midor I used arrays because in the original post there are also arrays.":) – Vlad from Moscow Nov 29 '14 at 14:10
  • @Vald: yup, but those are not limited in size, since the compiler will determine the right size at compile time. Also seems like a bad design decision from OP to use arrays there. Would make it possible to swap the argument that triggers add and exit at runtime in your code. Seems like the OP is doing this for exercise only, so it won't matter, but I think we should try to point him into the right direction right away. – midor Nov 29 '14 at 14:15
  • @Bryan Reyes It seems you did not copy and paste the code correctly. – Vlad from Moscow Nov 29 '14 at 16:01
  • @VladfromMoscow Hi, is it possible to input like this? "ADD 4,5" What I mean is that your ADD input is with the two numbers, not separating each other. Can you make it? – Bryan Reyes Nov 30 '14 at 07:28
  • @Bryan Reyes Yes, it is possible. Only then you need to use sscanf to extract each data from the string. – Vlad from Moscow Nov 30 '14 at 08:37