我正在做一個小的家庭作業,我應該做一個食物菜單。無論如何,我的開關不工作。我試圖用一個簡單的功能,我可以通過的「魚」,「喝」,或「碼片」的值,然後將輸出:用字符串切換語句?
"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");
}
在每種情況下,您應該使用'strcmp'來確保完整的字符串實際匹配。 – 2012-03-25 00:03:20
@R。正如我所說的,如果每個選擇在某個位置都有一個唯一的字母,這將只能正常工作。 – 2012-03-25 00:04:04
這是一個單獨的問題。我的觀點是,你的解決方案也會接受以相同字母開頭但不在列表中的其他字符串。 – 2012-03-25 00:08:05