查看單個文章
  #2  
舊 2014-02-20, 12:46 AM
a40136 a40136 目前離線
進階會員
 
註冊日期: 2007-07-01
文章: 261
預設

代碼:
#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了。
回覆時引用此篇文章