代碼:
#include<stdio.h>
#include<string.h>
#include <iostream>
using namespace std;
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;
}
i++放錯位置了,你輸入car會去比對dog,但是不符合,所以不會進if statement
所以i永遠會是0,會造成while不會中止,所已過一段時間程式就crash了。