Thread: C Program Help

  1. #1
    Registered User
    Join Date
    Dec 2016
    Posts
    6

    C Program Help

    This program should prompt the user for a series of letters. Then read in the letters and print out which dictionary words can be made from the provided letters. The dictionary is provided as a text file in the folder. But whenever I run it it doesn't give be anything. Can anyone help me figure out why this is happening?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define SIZE 99
    #define NUM_WORDS 100
    
    
    void find_frequency(char string[], int count[]);
    int compare_arrays(int dictionary[], int user[]);
    
    
    int main()
    {
        int total_words=11; //number of words
        char dictionary_words[NUM_WORDS][SIZE]; //store words in directory
        FILE *cfPtr; //dictionary.txt pointer
    
    
        if ((cfPtr=fopen("dictionary.txt","r"))==NULL)//try to open file
        {
            puts("File dictionary.txt could not be opened.");
            exit(1);//exit if file doesn't open
        }
    
    
        else{ //Read each word from the dictionary and save to array
            char line[SIZE]; //save each word
    
    
            {
                while(fgets(line,SIZE,cfPtr)!= NULL)
                      {
                          char*tokenPtr=strtok(line, "\t");
                          while(tokenPtr != NULL)
                            {
                                strcpy(dictionary_words[total_words],tokenPtr);
                                total_words++;
                                tokenPtr = strtok(NULL, "\t" );
                            }
    
    
                      }
            }
            }
        fclose(cfPtr);//close file
    
    
        char string[11];//get string of characters from user
        int count[26]={0};//store the number of each letter
    
    
        printf("Enter letters:\n");
        gets(string);
    
    
    
    
        find_frequency(string, count);//count of each character entered
    
    
        char temp[SIZE];
        int temp_count[26]={0};//convert words into letters
        int i;
    
    
        for(i=0; i<=total_words; i++);
        {
            strcpy(temp,dictionary_words[i]);
            find_frequency(temp,temp_count);//convert word to letters in alphabet
    
    
            if (compare_arrays(temp_count,count))//compare words with letters entered
            {
                printf("%s:", temp);//print what you can spell
            }
            else
            {
                 printf("broken", temp);
            }
           memset(temp_count,0,sizeof(temp_count));//test next word
        }
        return(0);
    }//end main
    
    
    //define function
    void find_frequency(char string[],int count[])
    {
        int i;
        for(i=0; string[i] != '\0'; i++)
        {
            if (string[i] >= 'a' && string[i] <= 'z')
            {
                count[string[i]-'a']++;
            }
        }
    }
    
    
    int compare_arrays(int dictionary[], int user[])
    {
        int j = 0;
    
    
        if (user[j] >= dictionary[j])
        {
            j++;
            if (j == 26)
            {
                return 0;
            }
        }
        return 1;
    }

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    You have an extraneous semicolon at the end of the for head on line 66 above.

    And your "compare_arrays" routine is kinda weird. It certainly isn't "comparing arrays". It doesn't even have a loop.

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    > gets(string);

    NEVER use gets(). It's not standard and a dangerous function due to buffer overflows. If you want to read a string as input to the console use scanf() with a string size allocate or better yet use fgets() to read the string, then you can make it more secure by using other functions such as sscanf().
    Double Helix STL

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    This code bears a remarkable resemblance to the code here. I sure hope you didn't pay for the code you posted.

  5. #5
    Registered User
    Join Date
    Dec 2016
    Posts
    6
    That code looks like its for the same assignment but its not the code that I used. No I did not pay to get this code.

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Good to hear. Did you make any progress?

  7. #7
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by chrisjn103 View Post
    That code looks like its for the same assignment but its not the code that I used. No I did not pay to get this code.
    If you paid it would probably be working.
    At least admit that you stole the non-working code from that site.

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by algorism View Post
    If you paid it would probably be working.
    One would hope.
    If not, running to a forum for help would be the next step.

    Quote Originally Posted by algorism View Post
    At least admit that you stole the non-working code from that site.
    The solution is actually heavily censored (requires payment to access), so I don't think they simply copy/pasted it. The (censored) solution code does have enough pieces visible to imply the similarity, especially constant and variable names. But I am willing to give the OP the benefit of the doubt, since I can't definitely verify the code was lifted.

  9. #9
    Registered User
    Join Date
    Dec 2016
    Posts
    6
    Quote Originally Posted by algorism View Post
    If you paid it would probably be working.
    At least admit that you stole the non-working code from that site.
    Why would I admit to something I did not do? Like I said , both codes are for the same assignment that's why they look similar. If you actually look at the code they are using it really isn't like mine. I have way more lines and most things are written out completely differently.

  10. #10
    Registered User
    Join Date
    Dec 2016
    Posts
    6
    Quote Originally Posted by Matticus View Post
    Good to hear. Did you make any progress?
    Somewhat. it seems like something in my compare arrays section isn't working correctly. I added an else statement to it that prints if its broken and it keeps printing that no matter what I enter.

  11. #11
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by chrisjn103 View Post
    Why would I admit to something I did not do? Like I said , both codes are for the same assignment that's why they look similar. If you actually look at the code they are using it really isn't like mine. I have way more lines and most things are written out completely differently.
    Obviously it's for the same program.
    Why would you steal code for a different program?
    I never said you didn't change or add anything.
    But look at all the points where it matches your code exactly.
    Clearly you took this code as a starting point.

  12. #12
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by chrisjn103 View Post
    Somewhat. it seems like something in my compare arrays section isn't working correctly. I added an else statement to it that prints if its broken and it keeps printing that no matter what I enter.
    Keep trying to get it working. If you get absolutely stuck, post your updated code along with information on what's still wrong.

  13. #13
    Registered User
    Join Date
    Dec 2016
    Posts
    6
    Quote Originally Posted by Matticus View Post
    Keep trying to get it working. If you get absolutely stuck, post your updated code along with information on what's still wrong.

    Still have no idea what to do. I've tried a few things and nothing seems to work. I tried getting it to print out what I'm trying to save as "temp" for the compare but it just keeps printing out blank lines. Here's the code again but I don't think it has many changes. I only kept 2 or 3 things.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define SIZE 99
    #define NUM_WORDS 100
    
    
    void find_frequency(char string[], int count[]);
    int compare_arrays(int dictionary[], int user[]);
    
    
    int main()
    {
        int total_words=0; //number of words
        char dictionary_words[NUM_WORDS][SIZE]; //store words in directory
        FILE *cfPtr; //dictionary.txt pointer
    
    
        if ((cfPtr=fopen("dictionary.txt","r"))==NULL)//try to open file
        {
            puts("File dictionary.txt could not be opened.");
            exit(1);//exit if file doesn't open
        }
    
    
        else{ //Read each word from the dictionary and save to array
            char line[SIZE]; //save each word
            char store[SIZE];
    
    
            {
                while(fgets(line,SIZE,cfPtr)!= NULL)
                      {
                          char*tokenPtr=strtok(line, "\t");
                          while(tokenPtr != NULL)
                            {
                                strcpy(dictionary_words[total_words],tokenPtr);
                                total_words++;
                                tokenPtr = strtok(NULL, "\t" );
                            }
    
    
                      }
            }
            }
        fclose(cfPtr);//close file
    
    
        char string[11];//get string of characters from user
        int count[26]={0};//store the number of each letter
    
    
        printf("Enter letters:\n");
        scanf("%s", string);
    
    
    
    
        find_frequency(string, count);//count of each character entered
    
    
        char temp[SIZE];
        int temp_count[26]={0};//convert words into letters
        int i;
    
    
        for(i=0; i<=total_words; i++);
        {
            strcpy(temp,dictionary_words[i]);
            find_frequency(temp,temp_count);//convert word to letters in alphabet
    
    
            if (compare_arrays(temp_count,count))//compare words with letters entered
            {
                printf("%s:", temp);//print what you can spell
            }
            else
            {
                 printf("broken");
            }
           memset(temp_count,0,sizeof(temp_count));//test next word
        }
        return(0);
    }//end main
    
    
    //define function
    void find_frequency(char string[],int count[])
    {
        int i;
        for(i=0; string[i] != '\0'; i++)
        {
            if (string[i] >= 'a' && string[i] <= 'z')
            {
                count[string[i]-'a']++;
            }
        }
    }
    
    
    int compare_arrays(int dictionary[], int user[])
    {
        int j = 0;
    
    
        while (user[j] >= dictionary[j])
        {
            j++;
            if (j == 26)
            {
                return 1;
            }
        }
        return 0;
    }
    Last edited by chrisjn103; 12-14-2016 at 02:58 PM.

  14. #14
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I don't have much time to review your code at the moment, but you should re-read the first sentence in post #2. This seems to make your code do something approaching what it intends.

  15. #15
    Registered User
    Join Date
    Dec 2016
    Posts
    6
    Wow yeah I thought I had fixed that. That seems to fix the main issue

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 03-15-2016, 03:29 PM
  2. Replies: 4
    Last Post: 12-21-2015, 07:17 AM
  3. Replies: 2
    Last Post: 09-09-2014, 02:36 PM
  4. Replies: 2
    Last Post: 12-11-2012, 12:25 AM
  5. Replies: 1
    Last Post: 03-03-2009, 04:47 PM

Tags for this Thread