Search code examples
cfunctionwhile-loopdynamic-memory-allocation

While loop exiting prematurely


So this program is supposed to unjumble a word to show valid words of the same length. This was an assignment that required dynamic memory allocation within functions and got rather messy. In the beginning of the program I have to dynamically allocate memory for a dictionary of length n. The dictionary we were given was length 10000 and the max length for words was 20 characters. I chose to dynamically create an array of permutations of each word then compare them to words in the dictionary with a binary search using strcmp (All words entered in by the user and in the dictionary were all caps making this a viable method). The entire program worked through the first iteration minus duplicate permutations that were valid words and the while loop exiting prematurely. I use the function Welcome() at the end of the while loop to ask whether or not the user would like to unscramble another word and it prints everything in that function but does not allow me to scan in a value for choice.

Some other specs:

  • user enters "y" or "Y" to denote they would like to play

I have not tried anything to solve this because I can not think of any reason that a while loop would just exit. Especially because choice should still be "y" or "Y" even if I am not allowed to scan in a new value.

This is main.

    int main() {


        char** dictionary;
        int num_words;

        // defined FILE* outside of file read in function to avoid returning local variable address
        FILE* ifp = fopen("Dictionary.txt", "r");

        fscanf(ifp, "%d", &num_words);

        dictionary = FileReadIn(num_words, ifp);

        char choice = Welcome();

        // main part of program Im unsure why it exits after its first iteration
        while ((choice == 'y') || (choice == 'Y')) {

            char* letters = LettersReadIn();

            // Calculates number of permutations which is (numletters)!
            long num_permutations = Factorial(strlen(letters));

            // Sets up permutations through malloc
            char** permutations = (char**)malloc((num_permutations) * sizeof(char*));

            int i;
            for (i = 0; i < num_permutations; i++) {
                permutations[i] = (char*)malloc(MAXWORDLENGTH * sizeof(char));
            }

            // Creates all permutations of the letters entered in by the user in a recursive function
           RecursivePermute(letters, 0, permutations);

           // Created the WordIndices array in order to keep track of which indices in the permutations array are valid words
           // Could not get the program to work when I created wordindices in Viable Words
           // Viable Words checks each permutation against words in the dictionary using a binary search 
           // which compared each word lexicographically with strcmp
           int* word_indices = (int*)malloc(num_permutations * sizeof(int));
           ViableWords(permutations, dictionary, num_permutations, num_words, word_indices);

           // Prints each index in permutations that is a valid word
           // valid word indices stored in word indices
           PrintWords(permutations, num_permutations, letters, word_indices);

           // frees permutations and word indices
           free(permutations);
           free(word_indices);

           choice = Welcome();
     } // End While loop

return 0;
} // End main

And this is the welcome function.

    char Welcome() {

         printf("Welcome to the Jumble Puzzle Solver!\n");
         printf("Would you like to enter a jumbled word?\n");

         char choice;
         scanf("%c", &choice);

   return choice;
   }// End Welcome

So essentially my questions are why causes this to occur and what is a solution.

If you need any other information please let me know, also if you see anything else that you would improve please let me know as well. I am fairly new to programming so I would love some constructive criticism.


Solution

  • The reason is scanf leaves a newline character in the input buffer after reading the input which is not cleared before reading the next character.

    Use getchar() to consume the newline character:

    scanf("%c", &choice);
    getchar(); 
    

    Or tell scanf() to ignore the whitespaces in the input buffer by using a leading space in the format string:

    scanf(" %c", &choice);