c: main function stops running when function called

Viewed 140

I am trying to code a hangman game in c (one of my first c programs) and I have a problem where the main function does not output anything when I call my runGame() function. The terminal doesnt terminate either so I thought it couldve been an infite loop in runGame(), but it doesnt even output the first printf line from runGame(). I am currently generating a random word from a text file through the randomWord() function and when I call this in the main function and not runGame(), it seems to work perfectly fine.

Does anyone know what may have caused the runGame() function to make the program act weirdly?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define maxLen 100

char *randomWord() { //generates a random word from txt file

    FILE *word_list;
    size_t line_no = 0;
    size_t select_len;
    //double drand48();
    char chosen_word[9895]; //size represents total number of lines
    char current_word[9895];
    chosen_word[0] = '\0'; //if no lines then prevents crashing
    int max = 1.0;

    //reading in file with words to choose from
    word_list = fopen("10000-most-common-eng-words.txt", "r");
    if (word_list == NULL) {
        printf("file not found\n");
        exit(EXIT_FAILURE);
    }

    while(fgets(current_word, sizeof(current_word), word_list)) { //while there are still lines to read
        if( (double)rand()/ ((RAND_MAX) / max) < 1.0 / ++line_no) { // odds of word being replaced given a random double
            strcpy(chosen_word, current_word); //replace with new word
        }
    }

    fclose(word_list); //close file
    select_len = strlen(chosen_word); //get word length of random chosen word
    if(select_len > 0 && chosen_word[select_len - 1] == '\n') { //if chosen word if a new line
        chosen_word[select_len - 1] = '\0'; //then replace with null value
    }

    printf(chosen_word);
    printf("\n");

    return strdup(chosen_word);
}

int runGame() {

    printf("hey"); //test

    int game_over = 0;
    int man_hung = 0;
    int rand_word_len;
    //char player_guess[1];
    char guesses_made[10];
    char player_guess;
    char rand_word[maxLen];

    printf("running game...\n");

    while(!game_over) {
        strcpy(rand_word, randomWord());
        //rand_word = randomWord(); //get random word
        rand_word_len = strlen(randomWord());
        printf("Enter your guess (single letters only): \n");
        player_guess = getchar(); //get length of random word
        while(man_hung != 10) {
            for(int a = 0; a < sizeof(guesses_made) / sizeof(guesses_made[0]); a++) { //for every value in array
                if(guesses_made[a] == player_guess) { //see if guess has already been made
                    printf("You've already made that guess. Try something different...\n");
                    continue; //go back to second while loop (man_hung)
                } else { //else append guess to array
                    strcat(guesses_made, &player_guess);
                }
            }

            //iterate through random word array to see if letter exists in word
            for(int b = 0; b < sizeof(rand_word) / sizeof(rand_word[0]); b++) {
                if(rand_word[b] != player_guess) {
                    man_hung += 1;
                    printf("Sorry wrong guess, try again\n");
                    continue;
                }
                else { // delete letter from random word array
                    for(int c = b-1; c < rand_word_len; c++) {
                        rand_word[c] = rand_word[c+1];
                    }
                }
            } //end of for

            printf("Correct guess!");
            for(size_t i = 0; i < rand_word_len; ++i){
                if(rand_word[i] != 0) { //if element is not 'empty'
                    continue; //ask for another input
                }
            }

            printf("Well done,you won! The word was %s \n", rand_word);
            game_over = 1;
            break;


        }//end of man hung while
    }//end of game_over while

    return 0;
}//end of run_game

int main() {

    printf("uh"); //test
    runGame();

    printf("yes"); //test
    srand(time(NULL));

    return 0;
}
1 Answers

Some of the for loop are while loops here.
while ( guesses_made[a]) { will be true until a is the index of the terminating zero.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define maxLen 100

char *randomWord ( ) { //generates a random word from txt file

    FILE *word_list;
    size_t line_no = 0;
    char chosen_word[maxLen] = "";
    char current_word[maxLen] = "";
    int max = 1.0;

    //reading in file with words to choose from
    word_list = fopen ( "10000-most-common-eng-words.txt", "r");
    if ( word_list == NULL) {
        perror ( "10000-most-common-eng-words.txt");
        exit ( EXIT_FAILURE);
    }

    while ( fgets ( current_word, sizeof current_word, word_list)) { //while there are still lines to read
        if( (double)rand( ) / ((RAND_MAX) / max) < 1.0 / ++line_no) { // odds of word being replaced given a random double
            strcpy ( chosen_word, current_word); //replace with new word
        }
    }

    fclose ( word_list); //close file
    chosen_word[strcspn ( chosen_word, "\n")] = 0;//remove newline

    printf ( "%s\n", chosen_word);
    printf ( "\n");

    return strdup ( chosen_word);//memory allocated
}

int runGame ( ) {

    printf ( "hey"); //test

    int game_over = 0;
    int man_hung = 0;
    char guesses_made[11] = "";
    char player_guess = 0;
    char rand_word[maxLen] = "";
    char *word_copy = NULL;

    printf ( "running game...\n");

    while ( ! game_over) {
        word_copy = randomWord ( );//assign memory allocation
        strcpy ( rand_word, word_copy);
        while ( man_hung < 10) {
            printf ( "Enter your guess (single letters only): \n");
            scanf ( " %c", &player_guess);
            man_hung += 1;
            int a = 0;
            while ( guesses_made[a]) {//not terminating zero
                if ( guesses_made[a] == player_guess) {
                    printf ( "You've already made that guess. Try something different...\n");
                    break;
                }
                ++a;
            }
            if ( ! guesses_made[a]) {//terminating zero
                guesses_made[a] = player_guess;//add guess
                guesses_made[a + 1] = 0;//terminate
            }

            //iterate through random word array to see if letter exists in word
            int b = 0;
            int match = 0;
            while ( rand_word[b]) {// not terminating zero
                if( rand_word[b] == player_guess) {
                    match = 1;//found
                    int c = b;
                    // delete letter from random word array
                    while ( rand_word[c]) {
                        rand_word[c] = rand_word[c+1];
                        ++c;
                    }
                    --b;//for consecutive matching letters <-tt
                }
                ++b;
            } //end of for
            if ( ! match) {
                printf ( "Sorry wrong guess, try again\n");
            }
            else {
                printf ( "Correct guess!");
            }
            if ( rand_word[0]) { //if element is not 'empty'
                if ( 10 > man_hung) {
                    continue; //ask for another input
                }
            }
            else {
                printf ( "Well done,you won!\n");
            }
            printf ( "The word was %s \n", word_copy);
            free ( word_copy);//free allocated memory
            game_over = 1;
            break;
        }//end of man hung while
    }//end of game_over while

    return 0;
}//end of run_game

int main ( void) {

    srand ( time ( NULL));
    printf ( "uh"); //test
    runGame ( );

    printf ( "yes\n"); //test

    return 0;
}
Related