0

My speller problem compiles and, as far as I know it, I can load and unload the dictionary correctly, but I'm not getting the expected output with check. I'm pretty inexperienced with code (CS50 is my first time messing around with it) so I'm having a hard time actually finding where my problem is

Here's my code for check:

bool check(const char *word)
{
    // create cursor pointer
    struct node *cursor = root;
    FILE *wrdCheck = fopen(word, "r");
    if(wrdCheck == NULL)
    {
        return false;
    }

    while(feof(wrdCheck) == 0)
    {
        char t = fgetc(wrdCheck);
        //checks if its alphabetical letter
        if(isalpha(t))
        {
            t = toupper(t);
            //checks if existis
            if(cursor -> children[t - 'A'] == NULL)
            {
                cursor = root;
                return false;
            }
            //moves cursor
            else
            {
                cursor = cursor -> children[t - 'A'];
            }
        }
        // checks if apostrophe character
        else if(t == '\'')
        {
            // checks if exists
            if(cursor -> children[26] == NULL)
            {
                cursor = root;
                return false;
            }
            //moves cursor
            else
            {
                cursor = cursor -> children[26];
            }
        }
        // checks if its any other character (end of word)
        else
        {
            if(cursor -> is_word == true)
            {
                cursor = root;
                return true;
            }
            else
            {
                cursor = root;
                return false;
            }
        }
    }

    //closes file
    fclose(wrdCheck);
    return false;
}
2
  1. Please state in which way your output differs. Number of loaded words? Number of mismatches?
  2. feof does not tell you whether you are at the end of the file. It tells you that you are at the end of the file after you've failed to read something. So it has to be placed after a read operation, not before.
  3. check should not open any files. It should use the dictionary structure load has created/populated. You can use a for loop to iterate word, like

        for(int pos = 0; word[pos] != '\0'; pos++)
        {
            char t = word[pos];
            //checks if its alphabetical letter
            if(isalpha(t))
            {
                t = toupper(t);
                ...
    
2
  • 1. My output differs as in there is no output for example Check50 shows me this:<br/> :( handles max length (45-char) words expected "MISSPELLED WORD...", not "" 2 and 3. Thanks! I am no longer using fopen for my function and trating word as a pointer to a word, the rest of my code remains the same, but I' still getting no output. – Gabriel Luz Carpes Sep 18 '17 at 13:45
  • 1
    Don't use only check50 to test. Does it work when compiled/called locally (not by check50)? Also, while you've posted your check, maybe the load would be interesting, too. – Blauelf Sep 18 '17 at 13:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .