Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch statement with strings?

Tags:

c

I'm working on a small homework assignment and I'm supposed to make a food menu. Anyways, my switch isn't working. I'm trying to use a simple function that I can pass a value of "fish", "drink", or "chips" to and then it will output:

"Are you ordering FISH?" (or chips/drink)  

I can't get the switch to work, it's supposed to detect what I pass into it and then output a printf based on the switch case.

CODE:

#include <stdio.h>

void menu() {
    printf("\nWelcome to Sunny FISH & CHIPS!\n\n");
    printf("########     Fish :     Haddock(K) Large(L) | $5.00\n");
    printf("# FOOD #                Halibut(T) Large(L) | $4.00\n");
    printf("########     Chips:     Cut(C)     Large(L) | $2.00\n");
    printf("                        Ring(R)    Large(L) | $3.00\n");
    printf("                                            | \n");
    printf("##########   Soft Drinks(S)        Large(L) | $2.00\n");
    printf("# DRINKS #   Coffee(C)             Large(L) | $1.75\n");
    printf("##########   Tea(T)                Large(L) | $1.50\n");
    printf("---------------------------------------------\n");
    printf("Note: Medium price: 80%% of large.\n");
    printf("       Small price: 60%% of large.\n");
    printf("TAX is 10%%.\n");
    printf("More than 5 fish, 10%% discount on drink.\n");
    printf("Every 10 fish purchased, get 1 free softdrink.\n");
    printf("  - size of drink is according to size of fish\n");
}

void question (char choice[5]) {
    switch (choice[5]) 
    {
        case choice["fish"]:
            printf("Do you order FISH?\n");
        case choice["drink"]:
            printf("Do you order CHIPS?\n");
        case choice["chips"] :
            printf("Do you order DRINKS?\n");
        default :
            printf("Enter a valid choice: \n");
    }
}

main() {

    // menu();
    question("fish");

}
like image 420
eveo Avatar asked Nov 29 '22 03:11

eveo


2 Answers

C does not support that kind of switch, but if it would, the syntax would be

 switch(choice)
 {
    case "fish":
        something();
        break;
    case "drink":
        other_thing();
        break;
 }

A switch to me is often clearer than a (long) list of if - else ifs. Even though in this case it seems overcomplicated, I would prefer approaches like this:

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

enum menu_items { FISH, DRINK, CHIPS, UNKNOWN };

struct items
{
  char *name;
  enum menu_items id;
} items_list[] = {
  { "fish", FISH },
  { "drink", DRINK },
  { "chips", CHIPS }
};

int main(void)
{
  int i;
  enum menu_items mid;
  struct items *choice = NULL;

  // ...

  for(i = 0, choice = NULL; i < sizeof items_list/sizeof (struct items); i++)
  {
    if (strcmp(answer, items_list[i].name) == 0)
    {
      choice = items_list + i;
      break;
    }
  }    

  mid = choice ? choice->id : UNKNOWN;

  // the following would be enough to obtain the output of your example;
  // I've not embodied the code into a func, but it's easy to do if you need
  if ( mid != UNKNOWN )
  {
      // the function a_func transforms the string of the food
      // e.g. to uppercase, or it could map it to whatever according to some
      // other data... or expand the struct to hold what you want to output
      // with "fish", "drink", "chips", e.g. choice->screen_name
      printf("Do you order %s?\n", a_func(choice->name));
  }
  else
  {
      printf("Enter a valid choice:\n");
  }
  // ---------

  // or if you prefer the switch you have something like:

  switch(mid)
  {
  case FISH:
    printf("fish\n");
    break;
  case DRINK:
    printf("drink\n");
    break;
  case CHIPS:
    printf("chips\n");
    break;
  default:
    printf("unknown choice\n");
    break;
  }

  return 0;
}

If you choose properly the approach, your code could be always the same and only your data grow.

like image 125
ShinTakezou Avatar answered Dec 21 '22 08:12

ShinTakezou


In addition to the other answers, if you find that your list of choices all begin with a unique letter (or have a unique letter in another position) then you can switch on that letter:

switch (choice[0]) {
case 'f':
    // they chose fish
    break;
case 'c':
    // they chose chips
    break;
case 'd':
    // they chose drink
}

This will be faster than using strcmp (although it doesn't matter for your case) and is less maintainable. However, it's good to know all the options and realise how you can use some of these things.

like image 27
Seth Carnegie Avatar answered Dec 21 '22 07:12

Seth Carnegie