Turn off statement with strings?

I am working on a little homework and I have to make a meal menu. In any case, my switch does not work. I try to use a simple function to pass the meaning of "fish", "drink" or "chips" and then outputs:

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

I cannot make the switch work, it must determine what I pass to it, and then printf based on the code of the switch.

THE 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"); } 
+4
source share
5 answers

In addition to other answers, if you find that your list of options starts with a unique letter (or has a unique letter in a different position), you can switch on this letter:

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

It will be faster than using strcmp (although it does not matter for your case) and less catered for. However, it’s good to know all the options and understand how you can use some of these things.

+6
source

You cannot use the switch with strings.

You can use strcmp to compare strings.

 if (strcmp(choice,"fish")==0) { //fish } else if (strcmp(choice,"drink")==0) { //drink } . . . 
+6
source

C does not support this type of switch, but if , the syntax will be

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

Switching to me is often clearer than the (long) if-else ifs list. Even if it seems complicated in this case , I prefers such approaches:

 #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 easy to do if you need if ( mid != UNKNOWN ) { // the function a_func transforms the string of the food // eg 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", eg 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 the right approach, your code can always be the same, and only your data will grow.

+6
source

C does not support line switches ... you should use strcmp()

+2
source

switch does not work like in C. You will need to construct the if and use strcmp() to compare the strings.

+1
source

Source: https://habr.com/ru/post/1403279/


All Articles