Using a switch() is not the recommended way to handle this — but you can do it. You will create a lookup-table (array of string literals of the valid shapes) and when you are checking if the user-input is a valid shape, you will save the index that corresponds to the valid shape and pass the index as the switch() variable. Then your cases will be integer values and you can take the appropriate action.

For example, you can create a simple lookup-table with:

/* lookup-table (array of pointers to string-literals) */
const char *shapes[] = { "circle", "square", "rectangle", "triangle" };

/* if you need a constant, #define one (or more) */
#define NSHAPES (int)(sizeof shapes / sizeof *shapes)

Where NSHAPES is the number of shapes in your shapes array (you could just use 4, but avoid hardcoding numbers … MagicNumbers…)

Then you can create a simple function to loop over the array comparing the shape entered by the user with each of the shapes in your lookup-table, returning the index if found, or -1 if not found, e.g.

/* function to validate shape is one of the valid shapes */
int validshape (const char *shape)
{
    for (int i = 0; i < NSHAPES; i++)           /* loop over each shape */
        if (strcmp (shape, shapes[i]) == 0)     /* compare shape with shapes[i] */
            return i;                           /* return index if found */
    
    return -1;                                  /* return -1 if no match found */
}

Then in main(), you take the user-input and simply pass the string to the function and validate, e.g.

    int shape = -1;
    ...
        shape = validshape (line);              /* get shape index (-1 if none) */
        if (shape != -1)                        /* check index is valid  */
            break;                              /* (break loop if valid) */
        /* otherwise handle error, show allowed */
        fputs ("  error   : invalid input.\n", stderr);

With the valid index in shape, you can then switch(shape), e.g.

    fputs ("\nvalid : ", stdout);
    switch (shape) {
        case 0: puts (shapes[0]); break;
        case 1: puts (shapes[1]); break;
        case 2: puts (shapes[2]); break;
        case 3: puts (shapes[3]); break;
        default: fputs ("oops -- shouldn't get here\n", stderr);
    }

You can update each of the cases to handle your arithmetic.

The full version (taking user input with fgets() so new C-programmers don’t fall into the many pitfalls associated with taking user-input with scanf(), you could do:

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

/* lookup-table (array of pointers to string-literals) */
const char *shapes[] = { "circle", "square", "rectangle", "triangle" };

/* if you need a constant, #define one (or more) */
#define NSHAPES (int)(sizeof shapes / sizeof *shapes)
#define MAXC 1024

/* simple function to show allowed shapes */
void showallowed (void)
{
    fputs ("  allowed :", stdout);              /* output prefix */
    for (int i = 0; i < NSHAPES; i++)           /* loop over each shape */
        printf (" %s", shapes[i]);              /* output shape */
    putchar ('\n');                             /* tidy up with newline */
}

/* function to validate shape is one of the valid shapes */
int validshape (const char *shape)
{
    for (int i = 0; i < NSHAPES; i++)           /* loop over each shape */
        if (strcmp (shape, shapes[i]) == 0)     /* compare shape with shapes[i] */
            return i;                           /* return index if found */
    
    return -1;                                  /* return -1 if no match found */
}

int main (void) {
    
    char line[MAXC];                            /* buffer to hold all user-input */
    int shape = -1;
    
    while (1) { /* loop continually */
        fputs ("\nenter shape: ", stdout);      /* output prompt for shape */
        if (!fgets (line, MAXC, stdin))         /* validate user input */
            return 1;
        line[strcspn (line, "\n")] = 0;         /* trim \n from end of line */
        shape = validshape (line);              /* get shape index (-1 if none) */
        if (shape != -1)                        /* check index is valid  */
            break;                              /* (break loop if valid) */
        /* otherwise handle error, show allowed */
        fputs ("  error   : invalid input.\n", stderr);
        showallowed();
    }
    
    fputs ("\nvalid : ", stdout);
    switch (shape) {
        case 0: puts (shapes[0]); break;
        case 1: puts (shapes[1]); break;
        case 2: puts (shapes[2]); break;
        case 3: puts (shapes[3]); break;
        default: fputs ("oops -- shouldn't get here\n", stderr);
    }
}

Example Use/Output

$ ./bin/validshapes

enter shape: apples
  error   : invalid input.
  allowed : circle square rectangle triangle

enter shape: squares
  error   : invalid input.
  allowed : circle square rectangle triangle

enter shape: rectangle

valid : rectangle

Let me know if you have further questions.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top