Thread: Error in loop

  1. #1
    Registered User
    Join Date
    Aug 2014
    Posts
    3

    Error in loop

    Good afternoon ladies and gentlemen. I would like someone to take a look at my loop to help me find the error that I am making. The purpose of this program is to unscramble the user's input. for example if the user entered 'ftooabll' the program would print 'football'

    Now, this only works for strings that are contained in the file wordlist. That being said, I would like this to repeat this search multiple times. Currently, the process is being repeated 7 times, but it only works on the first iteration. The code and sample input/output is below.

    Thank you very much for your help.


    (p.s. I have only been studying C for about a month, so please bear with me through this.)

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define NUMLOOP 6
    void sort_string(char*);
    int main()
    {
       char string[100];
       int i, j = 0;
       char string_1[100], string_2[100], string_3[100];
       FILE* wordlist;
       wordlist = fopen("wordlist.txt", "r");
       while (j <= NUMLOOP) {                                                // Start loop to ask for NUMLOOP different strings to search for in the wordlist
          printf("Enter string:\n");                                         // Prompt user for string.
          gets(string);                                                      // Place entered string into "string"
          sort_string(string);                                               // Call to function "sort_string" to alphabetize the characters in "string"
          while (fgets(string_1, sizeof string_1 , wordlist) != NULL)        // Begin loop to search wordlist for match & load wordlist into string_1
          {
             strcpy(string_2, string_1);                                     // Copy string_1 into string_2 to save original format of word
             sort_string(string_1);                                          // alphabetize each string in the wordlist
             int result = strcmp(string, string_1);                          // Compare entered string against the alphabetized wordlist
             if (!result)
             {
                printf("%s", string_2);                                      // If a match is found, print it.
             }
          }
          ++j;
       }
       return 0;
    }
    void sort_string(char *s)
    {
       int c, d = 0, length;
       char *pointer, *result, ch;
     
       length = strlen(s);
     
       result = (char*)malloc(length+1);
     
       pointer = s;
     
       for ( ch = '!' ; ch <= '}' ; ch++ )
       {
          for ( c = 0 ; c < length ; c++ )
          {
             if ( *pointer == ch )
             {
                *(result+d) = *pointer;
                d++;
             }
             pointer++;
          }
          pointer = s;
       }
    
       *(result+d) = '\0';
     
       strcpy(s, result);
       free(result);
    }
    Sample input/output:
    (note: all data has been verified to be in the wordlist)

    Enter string:
    4132dcba
    abcd1234
    Enter string:
    4132dcba
    Enter string:
    4132dcba
    Enter string:
    4132dcba
    Enter string:
    4132dcba
    Enter string:
    4132dcba
    Enter string:
    4132dcba

  2. #2
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    It's time to refactor, buddy. That's a tiny program but it already makes my head hurt trying to parse it.
    Refactor it, then I'll look at it, but my guess is that I won't have to, you'll probably discover your mistake yourself while your refactoring it.

    Anyway, you don't need to make sort_string() do all the work, C already has qsort().
    Last edited by Yarin; 09-24-2014 at 03:57 PM.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    The first time you get to the end of your "while()" loop, you're at the end of your file. Look into the "rewind()" function.



    Some more stuff...

    I noticed you use both "fgets()" and "gets()". "gets()" is unsafe and depreciated - I'd strongly recommend just using "fgets()".

    FAQ > Why gets() is bad / Buffer Overflows - Cprogramming.com
    FAQ > Get a line of text from the user/keyboard (C) - Cprogramming.com

    You also don't need to cast malloc:

    FAQ > Casting malloc - Cprogramming.com

    You should check the return value of "fopen()" before trying to access the file. If it fails to open properly, you don't want to try accessing it.

    (p.s. I have only been studying C for about a month, so please bear with me through this.)
    Keep up the good work!

  4. #4
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Some tips to help
    "gets()" is a very bad function to use.
    FAQ > Why gets() is bad / Buffer Overflows - Cprogramming.com

    Casting malloc is considered poor programming practice in the C language
    FAQ > Casting malloc - Cprogramming.com

    In the C language, it's a good idea to put "void" in your main declaration if you do not wish to have any arguments from the command line
    FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]) - Cprogramming.com



    [edit]
    You beat me to it!
    [/edit]
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Do/while loop error - C prg
    By Kent Luttrell in forum C Programming
    Replies: 3
    Last Post: 10-24-2012, 02:23 PM
  2. Replies: 3
    Last Post: 08-21-2012, 11:50 PM
  3. Error In a While Loop
    By thadis_4 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2012, 04:49 PM
  4. error (do-while)loop
    By prehisto in forum C Programming
    Replies: 8
    Last Post: 10-30-2011, 09:06 AM
  5. Loop seg error
    By Zishaan in forum Game Programming
    Replies: 2
    Last Post: 03-28-2007, 01:27 PM