tags:

views:

66

answers:

3

Please help me with a spellcheck program in C. The majority of the coding are complete (I think...). I'm really stuck because I'm not sure why the program wouldn't compile. Admittedly, I'm still an amateur coder, would you also provide a few suggestions on some of the bad coding habits that I have in the code? Thank you!

Error Message:

1>------ Build started: Project: project7, Configuration: Debug Win32 ------
1>Compiling...
1>project7.c
1>c:\users\x309\documents\visual studio 2008\projects\project7\project7\project7.c(16) : warning C4101: 'dictionaryWord' : unreferenced local variable
1>c:\users\x309\documents\visual studio 2008\projects\project7\project7\project7.c(77) : warning C4029: declared formal parameter list different from definition
1>c:\users\x309\documents\visual studio 2008\projects\project7\project7\project7.c(91) : warning C4013: 'strlen' undefined; assuming extern returning int
1>c:\users\x309\documents\visual studio 2008\projects\project7\project7\project7.c(96) : warning C4013: 'strncmp' undefined; assuming extern returning int
1>c:\users\x309\documents\visual studio 2008\projects\project7\project7\project7.c(101) : warning C4013: 'printf' undefined; assuming extern returning int
1>c:\users\x309\documents\visual studio 2008\projects\project7\project7\project7.c(78) : warning C4101: 'i' : unreferenced local variable
1>Linking...
1>project7.obj : error LNK2019: unresolved external symbol _artLength referenced in function _spellCheck
1>C:\Users\x309\Documents\Visual Studio 2008\Projects\project7\Debug\project7.exe : fatal error LNK1120: 1 unresolved externals
1>Build log was saved at "file://c:\Users\x309\Documents\Visual Studio 2008\Projects\project7\project7\Debug\BuildLog.htm"
1>project7 - 2 error(s), 6 warning(s)

What's required... There is only one stage on this project, writing the spellCheck routine. The spellCheck function has two parameters. The first parameter (article[]) is a pointer to an array of characters. The contents of this array are an article that you need to spell check. The end of the article is marked with the normal 0 (marking the end of a string). The article includes punctuation, upper and lower case words, numbers, and abbreviations. Your function must print every word in the article that cannot be found in the dictionary. The dictionary is the second parameter to the function (more on this later).

#include <stdio.h>
#include<string.h>

char dictionary[1000000];
char article[100000];

void spellCheck(char[], char[]);
int isLetter(char c);
void removePunc(char article[]);
void toLower( char article[]);
void lowerDictionary( char dictionary[]);
int artLength( char article[]);
void nextArticleWord(char article[], char articleWord[],  int artLength, char dictionary[]);

int main(void) {
    FILE* dict_file;
    FILE* article_file;
    int bytes_read;
    char* p;

    dict_file = fopen("american-english.txt", "r");
    if (dict_file == 0) {
        printf("unable to open dictionary file \"american-english.txt\"\n");
        return -1;
    }

    article_file = fopen("article.txt", "r");
    if (article_file == 0) {
        printf("unable to open file \"article.txt\"\n");
        return -1;
    }

    /* read dictionary */
    p = dictionary;
    p = fgets(p, 100, dict_file);
    while (p != 0) {
        while (*p != '\0') { 
            p += 1; 
        }
        p = fgets(p, 100, dict_file);
    }

    /* read article */
    p = article;
    bytes_read = fread(p, 1, 1000, article_file);
    p += bytes_read;
    while (bytes_read != 0) {
        bytes_read = fread(p, 1, 1000, article_file);
        p += bytes_read;
    }
    *p = 0;

    spellCheck(article, dictionary);
}   



int articlePosition =0;
int dictionaryPosition = 0;




void spellCheck(char article[], char dictionary[]) {
    char articleWord[50]; 
    char dictionaryWord[50];
    int articleLength = artLength(article);
    removePunc(article);
    toLower(article);
    lowerDictionary(dictionary);
    nextArticleWord(article, articleWord, articleLength, dictionary);

}

void nextDictionaryWord(char dictionary[], char dictionaryWord[]){
    int i;
    for(i =0; dictionary[dictionaryPosition] != '\n'; i++){
        dictionaryWord[i] = dictionary[dictionaryPosition];
        dictionaryPosition++;
    }
}
int isLetter(char c){
    if ( (c>='a'&&c<='z') || (c>='A'&&c<='Z'))
        return 1;
    return 0;
}

void removePunc(char article[]){
    int i, j=0;
    for ( i =0; article[i] != 0; i++){
        if (isLetter(article[i])){
            article[j] = article[i];
            j++;
        }
        else if (!isLetter(article[i])){
            article[j] = ' ';
            j++;
        }       
    }
}

void toLower( char article[]){
    int i=0;
    for( i; article[i] != 0; i++){
        if ( article[i] >= 'A' && article[i] <='Z')
            article[i] = article[i] + 32;
    }
}

void lowerDictionary( char dictionary[]){
    int i=0;
    for(i; dictionary[i] != 0; i++){
        if (dictionary[i] >= 'A' && dictionary[i] <= 'Z'){
            dictionary[i] = dictionary[i] + 32;
        }
    }
}


int articleLength( char article[] ){
    int count=0;
    while (article[count] != 0)
        count++;
    return count;
}

void nextArticleWord(char article[], char articleWord[],  int articleLength, char dictionaryWord[], char dictionary[]){
    int j, i;
check:
    while(!isLetter(article[articlePosition])){
        if (article[articlePosition] == 0){
            return;
        }
        articlePosition++;
    }
    for(j=0; article[articlePosition] != ' ' || articlePosition == articleLength; j++){
        articleWord[j] = article[articlePosition];
        articlePosition++;
    }   

    if (strlen(articleWord)<2){
        goto check;
    }
    articleWord[j+1] = 0;
    //dictionary search
        while (!strncmp(articleWord, dictionaryWord,strlen(articleWord))){
            nextDictionaryWord(dictionary, dictionaryWord);
        }
        if(strncmp(articleWord, dictionaryWord,strlen(articleWord)))
            return;
        printf(articleWord);
}
A: 

Not much information to go on - in future you need to post the actual error messages you are getting. However, you definitely need to move your function declarations so they appear in the code before the main() function.

anon
A: 

Have you tried going through the compiler errors and understanding what they mean?

From a cursory glance, here's where I think the problem is. The compiler error is:

error LNK2019: unresolved external symbol _artLength referenced in function _spellCheck

Which basically means the compiler is looking for a function called artLength, called from function spellCheck. However, it's not finding it.

It seems like you have a function called articleLength, which may be what you meant to write?

I would definitely advise you, however, to go over all the other output from your compiler and try to understand what it means. A lot of it is basically telling you that the compiler can't find certain functions, which is because you're calling them before you actually write them.

Edan Maor
+1  A: 

You have made a forward declaration:

int artLength( char article[]); 

but your actual implementation is:

int articleLength( char article[]); 

Make them identical (change either one of them) and your project will compile.

danbystrom