0

Im writing a dictionary program in C thus far my program opens a txt file(dictionary) and then opens another txt file to be checked for spelling it compares every word in the document with every word in the dictionary file at this point it prints out the first word that is spelled wrong in the document but, never goes back into the inner while loop im not shure how to fix this problem

here is the last part of my code Please HELP!

//compare document to dictionary 

char dictword[300]; 
char misword[300]; 
FILE *fp1, *fp2;

fp2 = fopen("dictionary.txt","r"); //open dictionary
fp1 = fopen("mispelled.txt","r"); //open document to be checked for spelling


while (test != EOF)//run loop until every  misword file has been checked
{
    test = fscanf(fp1,"%s",misword); //gets one word from the dictionary file

        while (test2 != EOF)//run loop until every word in dict file has been checked
        {
         test2 = fscanf(fp2,"%s",dictword); //gets one word from the dictionary file

            if (0==strcmp(misword,dictword))//if misword is = to dictword
                break;  //if a word in the misword file matches the dictionary word it must be spelled correct, break out of loop

                else if(test2 == EOF)//if one misword is compared with every dictword misword must not be a word 
                printf("%s   This word is not found in the dictionary \n",misword); //thus print it         
        }  

} //while (test != EOF);//run loop until every  misword file has been checked          
fclose(fp2);    
fclose(fp1);    
  • 1. Read this: http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong 2. What is a sample input and output that you are seeing fail ? – Fantastic Mr Fox Dec 09 '15 at 02:19
  • when calling the `fopen()` function, always check (!=NULL) the returned value to assure the operation was successful. – user3629249 Dec 09 '15 at 02:31
  • when calling the `fscanf()` function, always check the returned value against the number of format conversion specifiers (the numbers should match). A returned value of `EOF` is just one error condition while checking against the number of format conversion specifiers would catch all the error conditions. – user3629249 Dec 09 '15 at 02:33
  • 1
    the variables `test` and `test2` are not defined within the posted code and the variable `test` is being check before it is initialized – user3629249 Dec 09 '15 at 02:34
  • the comments on these two lines: `test = fscanf(fp1,"%s",misword); //gets one word from the dictionary file while (test2 != EOF)//run loop until every word in dict file has been checked` have me a bit confused. I thought that each dictionary word was being checked against each word in the file being tested. – user3629249 Dec 09 '15 at 02:36
  • strongly suggest controlling the outer `while()` loop with the call to `fscanf()`. I.E `while( 1 == (test = fscanf( fp1,"%299s",misword) ) ) )`. Similar considerations need to be given to the `while()` statement for the inner loop – user3629249 Dec 09 '15 at 02:46
  • whether the misspelled word is found in the dictionary file or not, the dictionary file needs to be set back to its' beginning for every search sequence. Perhaps by using: fseek( fp2, 0, SEEK_SET ); and of course, check the returned value from this call to assure the operation was successful – user3629249 Dec 09 '15 at 02:49
  • a call to `fscanf()` using a '%s" format specifier will not get past any white space. Suggest using: `while( 1 == (test = fscanf( fp1," %299s",misword) ) ) )` Notice the leading space char in the format literal. That space will cause any white space to be consumed. If the words in the two files contain anything but the words + white space, then the format literals need to be modified, for instance if either of the files contains .'. or ',', etc – user3629249 Dec 09 '15 at 02:55
  • @user3629249 - **The `fscanf` function**: … Input white-space characters (as specified by the `isspace` function) are skipped, unless the specification includes a `[`, `c`, or `n` specifier. – Armali Sep 13 '17 at 11:14

1 Answers1

1

Looks like your logic in the inner loop is wrong. You need to iterate over every word in the dictionary to see if it is spelled incorrectly. Once you find the word in the dictionary, you can break out of the inner loop like you do. But, before you break out of the inner loop, you should set a flag to indicate that you found the word. That way, after you finish the inner loop, you check the flag to see if the word was found or not. Also note that this implementation will be really slow. Often programs will put a dictionary into a hash table to make the lookups more efficient.

bruceg
  • 2,433
  • 1
  • 22
  • 29