0

wordCur 是一个大写字母的字符串,dictionary 是一个字符串数组,无论我在 wordCur 中输入什么,总是返回 0。

编辑:我稍微更新了代码,并为某些上下文添加了程序其余部分的删节版本。如此处所示,它只是在到达 checkValid 时崩溃

int main() {
    FILE *ifp;
    ifp = fopen("dictionary.txt", "r");
    int* lDist[26];
    int* lUsed[26];
    int dictLen;
    int i;
    fscanf(ifp, "%d", &dictLen);
    char dictionary[dictLen][7];
    char* letters[7];
    int scoreCur = 0;
    int scoreHi = 0;
    char wordCur[7];
    char wordHi[7];
    int isWord = 0;





//reads the dictionary into the array
for (i = 0; i < dictLen; i++) {
    fscanf(ifp, "%s", &dictionary[i]);
}
    scanf("%s", wordCur);
    isWord = checkValid(wordCur, dictLen, dictionary);
    if (isWord == 1) {
        scoreCur = calcScore(wordCur);
    }



//fclose(ifp); not sure why, but this causes a crash
return 0;
}


int checkValid (char *wordCur,int dictLen, char dictionary[dictLen]) {
    int valid = 0;
    int i;

    for (i = 0; i < dictLen; i++){
    int helper = strcmp(wordCur, dictionary[i]);
    if (helper = 0){
        valid = 1;
    }
}
4

3 回答 3

2

wordCur 是一串大写字母

int checkValid (char wordCur,int dictLen, char dictionary[dictLen])

不,wordCur是单个字符。不是字符串。C 中的字符串表示为一个字符数组,以一个值为 0 的字符终止。您需要一个指针参数char *wordCur.

于 2012-09-14T07:58:45.887 回答
0

wordCur 是一个大写字母的字符串,dictionary 是一个字符串数组

试试这个:

int checkValid (const char *wordCur,int dictLen, const char *dictionary[])

顺便说一句,即使你找到了你要找的东西,你也会继续搜索,而且对于字符串来说,comaprison 是错误的。我建议:

for (i = 0; i < dictLen; i++){  
    if (strcmp(wordCur, dictionary[i]) == 0){  
        valid = 1;  
        break;
    }  
}  
于 2012-09-14T07:57:00.090 回答
0

您的代码应该看起来更像这样:

int checkValid(const char *wordCur,       // word to search for (string)
               int dictLen,               // no of entries in dictionary
               char dictionary[][7])      // dictionary (array of strings)
{
    int valid = 0;
    int i;
    for (i = 0; i < dictLen; i++)
    {
        if (strcmp(wordCur, dictionary[i]) == 0)
        {
            valid = 1;
            break;
        }
    }
    return valid;
}
于 2012-09-14T08:06:19.213 回答