照著課本抄了一個翻譯功能的字串陣列,如下:
代碼:
#include<stdio.h>
#include<string.h>
char words[][2][40] = {
"dog", "xiao gou",
"car", "che zi",
"gold", "jin zi",
"child","hai zi",
"wife","qi zi",
"game","you xi",
"house","fang zi",
"rich","fa cai",
"",""
};
int main(void)
{
char english[80];
int i;
printf("enter english word: \n");
gets(english);
//look up the work
i=0;
//search while null string not yet encountered.
while(strcmp(words[i][0],"")) {
if(!strcmp(english,words[i][0])) {
printf("chinese translation: %s", words[i][1]);
break;
i++;
}
}
if(!strcmp(words[i][0],"")) printf("not in dictionary.\n");
return 0;
}
結果編譯沒問題,但執行時,則發生問題,也就是只有輸入第一個English的單字 dog 會輸出答案 xiao gou,如果輸入第二個以後的任何單字,則沒有反應,只剩游標在那裡一閃一閃的,也不會自動停止程式,似乎就當在那了?
不知以上的程式是哪邊出錯?
thanks.