2012-03-24 17 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'を使用して完全な文字列が実際に一致することを確認する必要があります。 –

+0

@R ..私が言ったように、これは各選択肢がある位置にユニークな文字を持っていれば正しく動作します。 –

+1

これは別の問題です。私の指摘は、同じ文字で始まるがリストにない他の文字列も、あなたのソリューションで受け入れることです。 –

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

C は私にスイッチがある場合の(長い)リストよりも、多くの場合、明確でないスイッチのようなものが、場合には、構文

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

になりますサポートしています - else 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; 
} 

は、あなたのコードは常に同じであることができ、あなただけのデータが育つ:この場合でもでけれどもそれは私がはこのようなアプローチを好むだろう、overcomplicatedようです。

+0

私はそれを読んだことは単なる宿題です... – ShinTakezou

関連する問題