Thread: Programming crashing error

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    32

    Question Programming crashing error

    Basically I'm supposed to make a spell check program and whatever the word is misspelled, it will print out the words from dictionary.txt to match the it (that's where the function will take its role..)

    But it keeps crashing whenever I write the first word.

    Can anyone help me why my program keeps crashing?





    Code:
    #include<stdio.h>
    #include<string.h>
    #define SIZE 40
    
    //Functions that will be used
    int substring (char shortstr[], char longstr []);
    int subsequence (char shortstr[], char longstr[]);
    int permutation (char string1[], char string2[]);
    int matchscore (char string1[], char string2[]);
    
    
    
    int main () {
        
            //This will read the txt.file
        FILE *ifp;
        ifp = fopen ("dictionary.txt", "r");
        
        int marker;
        char possible_words[3000][30];
        char dictionary[30000][30];
        char string2[30];
        int ans;
        char word[30];
        int i, max_of_words;
        int length1, length2;
        
    
        //This will read the max of words in the dictionary
        fscanf(ifp, "%d", &max_of_words);
     
        //I put the strings ina  here to read all of the words from the txt. file.
        for (i=0; i<max_of_words ; i++) {
            fscanf(ifp, "%s", dictionary[i]);
        }    
        
           
        printf("Welcome to the Spell Checker!\n");
        printf("The dictionary has been loaded\n\n");
      
        do {
        //resets the words for display
        //for(i=0; i<30000; i++)
         //strcpy(possible_words[i]," "); 
        
           printf("Please enter the word you would like checked\n");
           scanf("%s", word);
           printf("\n");
           
           length1 =strlen(word);
           marker=0;
           
           //This will set the loop whether the word matches the dictionary
    
           for (i=0; i<max_of_words ; i++) {
               strcpy(string2,dictionary[i]);
               length2 = strlen(string2);
               
                 if (strcmp(string2, word) == 0){                         
                   printf("Great, %s is in the dictionary!\n", word);
                   marker=1;
                 }    
                 
                 else if (length1==length2){
                      if (matchscore(word, string2) ==1 )
                        strcat(possible_words[i],string2);
                      else if (permutation(word, string2)==1)
                        strcat(possible_words[i], string2); 
                 }
                 else if (length1>length2) {
                      if (substring(string2, word) ==1 )
                        strcat(possible_words[i], string2);
                      else if (subsequence (string2, word) == 1)
                        strcat(possible_words[i], string2);
                 } 
                 else if (length1<length2) {
                      if (substring(word, string2)==1)
                        strcat(possible_words[i], string2);
                      else if (subsequence (word, string2) ==1)
                        strcat(possible_words[i], string2);
                 } 
           }   
            
         if (marker==0){
           for (i=0;i<max_of_words;i++) {
               printf("%s", possible_words[i]);
                if (strcmp(possible_words[i], "\0") != 0)
                 printf("\n");
           }    
         }          
            
           printf("Would you like to enter another word? (Yes=1/No=0)\n");
           scanf("%d", &ans);
    
        } while(ans!=0);
        
        fclose(ifp);
        system("PAUSE");
        return 0;
    }

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    For one, you are using strcat() on an uninitialized array of possible_words[i]. You should be using strcpy().
    Mainframe assembler programmer by trade. C coder when I can.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM