2012-03-24 59 views
4

我正在做一個小的家庭作業,我應該做一個食物菜單。無論如何,我的開關不工作。我試圖用一個簡單的功能,我可以通過的「魚」,「喝」,或「碼片」的值,然後將輸出:用字符串切換語句?

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

我不能讓交換機工作,它應該檢測我傳入它的內容,然後根據開關盒輸出printf。

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"); 

} 

回答

6

除了其他的答案,如果你發現你的選擇列表中的所有具有獨特的字母開頭(或在其他位置獨特的字母),那麼你可以switch上的那封信:

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

這將比使用strcmp(儘管對您的情況無關緊要)並且維護性較差。但是,瞭解所有選項並瞭解如何使用其中一些內容是很好的。

+0

在每種情況下,您應該使用'strcmp'來確保完整的字符串實際匹配。 – 2012-03-25 00:03:20

+0

@R。正如我所說的,如果每個選擇在某個位置都有一個唯一的字母,這將只能正常工作。 – 2012-03-25 00:04:04

+1

這是一個單獨的問題。我的觀點是,你的解決方案也會接受以相同字母開頭但不在列表中的其他字符串。 – 2012-03-25 00:08:05

2

C不支持交換機上串...你應該使用strcmp()

1

switch不一樣,在C.你需要做的工作一個if語句構造並使用strcmp()來比較字符串。

6

您不能在字符串中使用switch語句。

您可以考慮使用strcmp來比較字符串。

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

Ç支持該類型的交換機,但如果會,語法

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

切換到我往往比的,如果(長)列表清晰的 - 其他ifs。即使在這種情況下似乎過於複雜,我喜歡的方法是這樣的:

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

如果你選擇正確的方法,你的代碼可能是始終不變的,只有你的數據增長。

+0

我讀過它只是一個作業... – ShinTakezou 2012-03-24 23:32:06