Thread: Help with a program to spell check a file

  1. #76
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    NO!

    The problem is your unload function was never passed fp as a parameter - and fp is a local variable, so just
    Code:
    bool unload(FILE *fp) {
    
      if(fclose(fp)==0)
         return true;  //or TRUE
      else
        return false;  //or FALSE
    }
    Check if your program and compiler is set up to use true or TRUE and false or FALSE.

  2. #77
    Noobin to the max
    Join Date
    Mar 2013
    Posts
    73
    OH, ok, what about the rest of the errors???

  3. #78
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Dude22 View Post
    OH, ok, what about the rest of the errors???
    I have been unable to get back in here for awhile - database error and outgoing work unit from a F@Home work unit.

    I'll take a peek, but you should post your latest errors, because they will change as you change the program to fix other errors.

  4. #79
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    In this code you ended one function, but didn't put in the closing curly brace, before you started another function.

    Code:
    bool load(const char* dictionary)
    {
     
        int input(FILE *fp,char *words[DLENGTH],int getData);
    
    
        char **words = NULL;
        int count;
        char buff[BUFSIZ];  //BUFSIZ or BUFSIZE is a macro for your system - usually 256 or 512 char's in size. A "natural" buffer length, for your system.
     
        FILE *fp=fopen("DICTIONARY","r");
       
        count=input(fp,words,0);   //just counting this time
        rewind(fp);              //going back to the start of the file
     
        //malloc the right number of words here
        words=malloc(count * sizeof(char *));
        for(int i=0;i<count;i++) 
        {
            words[i]=malloc(DLENGTH * sizeof(char));  //#define LENGTH  29
        }
        input(fp,words,1);   //now getting the words
      
        //all the other stuff, here (mostly calling some functions)
        
        printf("%s\n",buff);
     
        return 0;
     //8888888888888888888888888888888888 Add the closing curly brace, here!!! 8888888888888
    
    
        int input(FILE *fp, char *words[DLENGTH], int getData)
       {
    Then re-compile and post up your current errors.

  5. #80
    Noobin to the max
    Join Date
    Mar 2013
    Posts
    73

    Hope I haven't messed to much up ;)

    Ok, so here is the program now:


    Code:
    #include <stdbool.h>
    #include <stdio.h>
    #include "dictionary.h"
    #include <string.h>
    #include <stdlib.h>
    
    
    #define MAXWORDS 26
    #define DLENGTH 29
    
    
    /**
     * Returns true if word is in dictionary else false.
     */
    bool check(const char* word, char *words[LENGTH], int n)
    {
    {
       int z;
       int j,lo=0,hi=n-1,mid;
       
       while(lo<=hi) 
       {
          mid=(lo+hi)/2; //printf("lo: %d  hi: %d  mid: %d\n",lo,hi,mid);getchar();
          j=strcmp(words[mid],text[z]);
          if(j>0)
          { 
             hi=mid-1;
          } 
          else if(j<0)
          {
             lo=mid+1;
          }
          else
          {
             return true;
          }
       }
       return false;
    }
    }
    /**
     * Loads dictionary into memory.  Returns true if successful else false.
     */
    bool load(const char* dictionary)
    {
     
        int input(FILE *fp,char *words[DLENGTH],int getData);
    
    
        char **words = NULL;
        int count;
        char buff[BUFSIZ];  //BUFSIZ or BUFSIZE is a macro for your system - usually 256 or 512 char's in size. A "natural" buffer length, for your system.
     
        FILE *fp=fopen("DICTIONARY","r");
       
        count=input(fp,words,0);   //just counting this time
        rewind(fp);              //going back to the start of the file
     
        //malloc the right number of words here
        words=malloc(count * sizeof(char *));
        for(int i=0;i<count;i++) 
        {
            words[i]=malloc(DLENGTH * sizeof(char));  //#define LENGTH  29
        }
        input(fp,words,1);   //now getting the words
      
        //all the other stuff, here (mostly calling some functions)
        
        printf("%s\n",buff);
     
        return 0;
     
        int input(FILE *fp, char *words[DLENGTH], int getData);
       { 
            int i=0;
            char buff[128];
            while((fgets(buff, BUFSIZ, fp)) != NULL) 
            {
                 if(getData) 
                 {
                      //remove the newline here
                      strcpy(words[i],buff);
                 }
            ++i;
            }
            if(getData==1)
                return i;
            else
                return -1;
       }
     
     
    }
    
    
    /**
     * Returns number of words in dictionary if loaded else 0 if not yet loaded.
     */
    unsigned int size(void)
    {
    if (count > 1)
        return count;
    else
        return 0;
    }
    /**
     * Unloads dictionary from memory.  Returns true if successful else false.
     */
    bool unload(FILE *fp)
    {
    //Is inptr the right thing to unload from memory???
    
    
        if(fclose(fp)==0)
        return true;
        else
        return false;
    }
    Here is my errors:

    Code:
    clang -ggdb -O0 -Qunused-arguments -std=c99 -Wall -Werror   -c -o dictionary.o dictionary.c
    dictionary.c:22:6: error: conflicting types for 'check'
    bool check(const char* word, char *words[LENGTH], int n)
         ^
    ./dictionary.h:22:6: note: previous declaration is here
    bool check(const char* word);
         ^
    dictionary.c:29:27: error: use of undeclared identifier 'buff'
          j=strcmp(words[mid],buff);
                              ^
    dictionary.c:77:17: error: use of undeclared identifier 'getData'
                 if(getData) 
                    ^
    dictionary.c:84:12: error: use of undeclared identifier 'getData'
            if(getData==1)
               ^
    dictionary.c:98:5: error: use of undeclared identifier 'count'
    if (count > 1)
        ^
    dictionary.c:99:12: error: use of undeclared identifier 'count'
        return count;
               ^
    dictionary.c:106:6: error: conflicting types for 'unload'
    bool unload(FILE *fp)
         ^
    ./dictionary.h:37:6: note: previous declaration is here
    bool unload(void);
         ^

    I hope I haven't messed to much up but what I have mainly been working on is "check". I was trying to get it to take the text input from speller.c (the other program that this one will be compiled with)

    I seem to have caused LOTS of conflicting type errors, however....

  6. #81
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Do NOT stack up your curly braces like this:
    Code:
    {
    {
    //grrrr!!
    
    }
    }
    Both ends of the check function.

    Not on the page you posted, but somewhere, you have another "check" function with a different declaration:
    Code:
    dictionary.c:22:6: error: conflicting types for 'check'    //this is in the file you posted above
    bool check(const char* word, char *words[LENGTH], int n)
         ^
    ./dictionary.h:22:6: note: previous declaration is here   //this is in the .h file
    bool check(const char* word);
    You can only have one check function, is what the compiler error is saying.


    You need to rebuild your project, because this error is not current with your code, above - but it has a big error, still:

    Code:
    j=strcmp(words[mid],text[z]); //WTF is text[z]?? delete it and put in 
    j=strcmp(words[mid],word);    //instead
    Last edited by Adak; 03-18-2013 at 12:30 AM.

  7. #82
    Noobin to the max
    Join Date
    Mar 2013
    Posts
    73
    oh ya, OK that is changed to just 1 brace on each side. But that does not change any of the errors....

  8. #83
    Noobin to the max
    Join Date
    Mar 2013
    Posts
    73
    Sorry, got to go, I will check back tomorrow morning, Thanks SO much for your help so far!!!

  9. #84
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You have to stop NOT closing out your functions with a closing brace.

    Code:
        int input(FILE *fp,char *words[DLENGTH],int getData);
     
     
        char **words = NULL;
        int count;
        char buff[BUFSIZ];  //BUFSIZ or BUFSIZE is a macro for your system - usually 256 or 512 char's in size. A "natural" buffer length, for your system.
      
        FILE *fp=fopen("DICTIONARY","r");
        
        count=input(fp,words,0);   //just counting this time
        rewind(fp);              //going back to the start of the file
      
        //malloc the right number of words here
        words=malloc(count * sizeof(char *));
        for(int i=0;i<count;i++)
        {
            words[i]=malloc(DLENGTH * sizeof(char));  //#define LENGTH  29
        }
        input(fp,words,1);   //now getting the words
       
        //all the other stuff, here (mostly calling some functions)
         
        printf("%s\n",buff);
      
        return 0;
    /// Requires a curly brace here! 888888888888888888  
    
    //Remove the semi-colon at the end of this next line of code! 
        int input(FILE *fp, char *words[DLENGTH], int getData); //****
       {
            int i=0;
            char buff[128];
            while((fgets(buff, BUFSIZ, fp)) != NULL)
            {
                 if(getData)
                 {
                      //remove the newline here
                      strcpy(words[i],buff);
                 }
            ++i;
            }
            if(getData==1)
                return i;
            else
                return -1;
       }
      
      
    }

  10. #85
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    ]I've discovered that some of the larger word lists have included more chemical (ie silly) and technical words, in them. Since that is very likely what your program will have to handle, increase the #define DLENGTH to 36.

    Attached is a practice file of 1,000 words that you can test your program with.

    [ATTACH]12616[/ATTACH
    Attached Files Attached Files

  11. #86
    Noobin to the max
    Join Date
    Mar 2013
    Posts
    73
    Ok, so I have made the changes, including changing the DLENGTH to 45 (as when I read the instructions again, it actually says that the dictionary that will be used to test the program, may have words up to 45 characters)

    Here are the errors now:

    Code:
    dictionary.c:22:6: error: conflicting types for 'check'
    bool check(const char* word, char *words[LENGTH], int n)
         ^
    ./dictionary.h:22:6: note: previous declaration is here
    bool check(const char* word);
         ^
    dictionary.c:101:5: error: use of undeclared identifier 'count'
    if (count > 1)
        ^
    dictionary.c:102:12: error: use of undeclared identifier 'count'
        return count;
               ^
    dictionary.c:109:6: error: conflicting types for 'unload'
    bool unload(FILE *fp)
         ^
    ./dictionary.h:37:6: note: previous declaration is here
    bool unload(void);
         ^
    It really doesn't seem to like it when variables are used in multiple functions... Would global variables fix this?

    In case it helps, here is my code now:


    Code:
    #include <stdbool.h>
    #include <stdio.h>
    #include "dictionary.h"
    #include <string.h>
    #include <stdlib.h>
    
    
    #define MAXWORDS 26
    #define DLENGTH 45
    
    
    /**
     * Returns true if word is in dictionary else false.
     */
    bool check(const char* word, char *words[LENGTH], int n)
    {
       int j,lo=0,hi=n-1,mid;
       
       while(lo<=hi) 
       {
          mid=(lo+hi)/2; //printf("lo: %d  hi: %d  mid: %d\n",lo,hi,mid);getchar();
          j=strcmp(words[mid],word);
          if(j>0)
          { 
             hi=mid-1;
          } 
          else if(j<0)
          {
             lo=mid+1;
          }
          else
          {
             return true;
          }
       }
       return false;
    }
    /**
     * Loads dictionary into memory.  Returns true if successful else false.
     */
    bool load(const char* dictionary)
    {
     
        int input(FILE *fp,char *words[DLENGTH],int getData);
    
    
        char **words = NULL;
        int count;
        char buff[BUFSIZ];  //BUFSIZ or BUFSIZE is a macro for your system - usually 256 or 512 char's in size. A "natural" buffer length, for your system.
     
        FILE *fp=fopen("DICTIONARY","r");
       
        count=input(fp,words,0);   //just counting this time
        rewind(fp);              //going back to the start of the file
     
        //malloc the right number of words here
        words=malloc(count * sizeof(char *));
        for(int i=0;i<count;i++) 
        {
            words[i]=malloc(DLENGTH * sizeof(char));  //#define LENGTH  29
        }
        input(fp,words,1);   //now getting the words
      
        //all the other stuff, here (mostly calling some functions)
        
        printf("%s\n",buff);
     
        return 0;
    }
        int input(FILE *fp, char *words[DLENGTH], int getData)
       { 
            int i=0;
            char buff[128];
            while((fgets(buff, BUFSIZ, fp)) != NULL) 
            {
                 if(getData) 
                 {
                      //remove the newline here
                      strcpy(words[i],buff);
                 }
            ++i;
            }
            if(getData==1)
                return i;
            else
                return -1;
       }
     
    
    
    /**
     * Returns number of words in dictionary if loaded else 0 if not yet loaded.
     */
    unsigned int size(void)
    {
    if (count > 1)
        return count;
    else
        return 0;
    }
    /**
     * Unloads dictionary from memory.  Returns true if successful else false.
     */
    bool unload(FILE *fp)
    {
    //Is inptr the right thing to unload from memory???
    
    
        if(fclose(fp)==0)
        return true;
        else
        return false;
    }

  12. #87
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Here's the problem with global variables -

    It's like finding a rotten egg - it's somewhere in your house, you can smell it! But where is it? A global rotten egg could be ANYWHERE in the whole house (even the attic), but a local rotten egg could only be in one room of the house. See?

    Global variables usually CAUSE problems, they don't solve them - exceptions exist of course.

    I'll look over your code.

    To handle 45 chars, you should set DLENGTH to 46 - because of the end of string char that all strings in C must have.

    Without it, chars are just a bunch of chars - not a string.


    edit:

    You have declared the check function twice - once in your .c program, and once in the .h header file. You need to change the name of the .c check to check2. (Or some other name that isn't being used by your program).

    Code:
    dictionary.c:22:6: error: conflicting types for 'check'
    bool check(const char* word, char *words[LENGTH], int n)
         ^
    ./dictionary.h:22:6: note: previous declaration is here
    bool check(const char* word);
    Problems here:
    1) You aren't calling this function
    2) count is an int, not an unsigned int in main(),
    3) I believe you're supposed to return TRUE or FALSE (a bool) here?
    4) you need to send count to size(), as a parameter (see below)

    Code:
    unsigned int size(void)   //should be unsigned int size(unsigned int count) 
    {
    if (count > 1)
        return count;         //don't you need to return TRUE or FALSE, so the return data type should be bool?
    else
        return 0;
    }
    Last error listed is like one of the earlier one's. You have declared a function in your .h file, AND again in your .c file. Change the name of the .c file function, or make the two into a single function, with only one declaration and prototype.

    Code:
    dictionary.c:109:6: error: conflicting types for 'unload'
    bool unload(FILE *fp)
         ^
    ./dictionary.h:37:6: note: previous declaration is here
    bool unload(void);
    When is the project due?
    Last edited by Adak; 03-18-2013 at 10:35 AM.

  13. #88
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Ok, so I have made the changes, including changing the DLENGTH to 45 (as when I read the instructions again, it actually says that the dictionary that will be used to test the program, may have words up to 45 characters)
    Then you should have made your DLENGTH 46, don't forget about the end of string character.

    Jim

  14. #89
    Noobin to the max
    Join Date
    Mar 2013
    Posts
    73
    I'm not sure if I should change the name of check (or any function), as it is called in the supplied program.... Below is the .h file


    Code:
    #ifndef DICTIONARY_H
    #define DICTIONARY_H
    
    
    #include <stdbool.h>
    
    
    // maximum length for a word
    // (e.g., pneumonoultramicroscopicsilicovolcanoconiosis)
    #define LENGTH 45
    
    
    /**
     * Returns true if word is in dictionary else false.
     */
    bool check(const char* word);
    
    
    /**
     * Loads dictionary into memory.  Returns true if successful else false.
     */
    bool load(const char* dictionary);
    
    
    /**
     * Returns number of words in dictionary if loaded else 0 if not yet loaded.
     */
    unsigned int size(void);
    
    
    /**
     * Unloads dictionary from memory.  Returns true if successful else false.
     */
    bool unload(void);
    
    
    #endif // DICTIONARY_H
    Is the problem because both the .h file and the .c file declare (const char* word)...

    This project has no specific end date, only this program, one other one in c that works with Huffman compression, and one basic one in HTML (i am vary familiar with html so that one will go quite quickly). These 3 programs have to be done by April 15th.
    Last edited by Dude22; 03-18-2013 at 10:45 AM.

  15. #90
    Noobin to the max
    Join Date
    Mar 2013
    Posts
    73
    Ok, so I have made all the changes you recommended, BUT I am not so sure about changing the name of the functions, as they are called in speller.c.... Also, they are called in the .h file.....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Spell checking program
    By JYorke2097 in forum C Programming
    Replies: 3
    Last Post: 01-15-2009, 08:28 PM
  2. how to check a file for every 15 mins thr' program
    By nitinmhetre in forum Linux Programming
    Replies: 10
    Last Post: 01-05-2007, 01:53 AM
  3. Spell Checker
    By DeepFyre in forum Tech Board
    Replies: 2
    Last Post: 02-11-2005, 12:17 PM
  4. spell check in C using a dictionary file
    By goron350 in forum C Programming
    Replies: 10
    Last Post: 11-25-2004, 06:44 PM
  5. I can't spell...
    By Cheeze-It in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 05-08-2003, 08:07 AM