简体   繁体   中英

When trying to run program on eclipse it crashes

I think it may be a memory leak of some kind when trying to read the .txt file. It just stops responding and crashes I am fairly new to programming in general.

This is the total incomplete code :

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

#define MAXNUMWORDS 100
#define MAXWORDLENGTH 60

void gallows(int i) {
switch (i) {
case 0:
    printf("Amount of wrong letters: %d\n\n", i);
    printf("\n");
    printf("\n");
    printf("\n");
    printf("\n");
    printf("\n");
    printf("\n");
    printf("____________\n\n");
    break;
case 1:
    printf("Amount of wrong letters: %d\n\n", i);
    printf("\n");
    printf("  |\n");
    printf("  |\n");
    printf("  |\n");
    printf("  |\n");
    printf("  |\n");
    printf("__|_________\n\n");
    break;
case 2:
    printf("Amount of wrong letters: %d\n\n", i);
    printf("  _______\n");
    printf("  |\n");
    printf("  |\n");
    printf("  |\n");
    printf("  |\n");
    printf("  |\n");
    printf("__|_________\n\n");
    break;
case 3:
    printf("Amount of wrong letters: %d\n\n", i);
    printf("  _______\n");
    printf("  |/\n");
    printf("  |\n");
    printf("  |\n");
    printf("  |\n");
    printf("  |\n");
    printf("__|_________\n\n");
    break;
case 4:
    printf("Amount of wrong letters: %d\n\n", i);
    printf("  _______\n");
    printf("  |/   | \n");
    printf("  |    O \n");
    printf("  |\n");
    printf("  |\n");
    printf("  |\n");
    printf("__|_________\n\n");
    break;
case 5:
    printf("Amount of wrong letters: %d\n\n", i);
    printf("  _______\n");
    printf("  |/   | \n");
    printf("  |    O \n");
    printf("  |    |\n");
    printf("  |    |\n");
    printf("  |\n");
    printf("__|_________\n\n");
    break;
case 6:
    printf("Amount of wrong letters: %d\n\n", i);
    printf("  _______\n");
    printf("  |/   | \n");
    printf("  |    O \n");
    printf("  |   \\|\n");
    printf("  |    | \n");
    printf("  |\n");
    printf("__|_________\n\n");
    break;
case 7:
    printf("Amount of wrong letters: %d\n\n", i);
    printf("  _______\n");
    printf("  |/   | \n");
    printf("  |    O \n");
    printf("  |   \\|/\n");
    printf("  |    | \n");
    printf("  |\n");
    printf("__|_________\n\n");
    break;
case 8:
    printf("Amount of wrong letters: %d\n\n", i);
    printf("  _______\n");
    printf("  |/   | \n");
    printf("  |    O \n");
    printf("  |   \\|/\n");
    printf("  |    | \n");
    printf("  |   /\n");
    printf("__|_________\n\n");
    break;
case 9:
    printf("Amount of wrong letters: %d\n\n", i);
    printf("  _______\n");
    printf("  |/   | \n");
    printf("  |    O \n");
    printf("  |   \\|/\n");
    printf("  |    | \n");
    printf("  |   / \\\n");
    printf("__|_________\n\n");
    break;
case 10:
    printf("Amount of wrong letters: %d\n\n", i);
    printf("  _______\n");
    printf("  |/   | \n");
    printf("  |    X \n");
    printf("  |   \\|/\n");
    printf("  |    | \n");
    printf("  |   / \\\n");
    printf("__|_________\n\n");
    break;
}

} int main() {

        printf("Welcome to Hangman!\n");
        printf("   /-----| \n");
        printf("   |       \n");
        printf("   |       \n");
        printf("   |       \n");
        printf("   |       \n");
        printf(" __|______\n");

        srand(time(NULL));

        char WordList[MAXNUMWORDS][MAXWORDLENGTH];
        int WordsReadIn = 0;

        FILE *pToFile = fopen("dictionary.txt", "r");

        if (pToFile == NULL) {
            printf("Failed To Open File\n");
            return 0;
        }

        char input[100];

        while (fgets(input, 100, pToFile)) {
            sscanf(input, "%s", WordList[WordsReadIn]);

        }

        printf("Total Words Read In:%d\n", WordsReadIn);

        fclose(pToFile);

        // index for random word
        int randomIndex = rand() % WordsReadIn;

        int numLives = 5;
        int numCorrect = 0;
        int oldCorrect = 0;

        int lengthOfWord = strlen(WordList[randomIndex]);

        //                       0 1 2 3 4 5
        //                       p u r p l e
        //  letterGuessed[8] = { 0,0,0,0,0,0,0,0 };
        int letterGuessed[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };

        int quit = 0;

        int loopIndex = 0;
        int reguessed = 0; // EDIT

        char guess[16];
        char letterEntered;

        // game loop    
        while (numCorrect < lengthOfWord) {

            printf("\n\nNew Turn....\nHangman Word:");

            for (loopIndex = 0; loopIndex < lengthOfWord; loopIndex++) {

                if (letterGuessed[loopIndex] == 1) {
                    printf("%c", WordList[randomIndex]    [loopIndex]);
                } else {
                    printf("-");
                }

            }

            printf("\n");

            printf("Number Correct So Far:%d\n", numCorrect);
            printf("Enter a guess letter:");
            fgets(guess, 16, stdin);

            if (strncmp(guess, "quit", 4) == 0) {
                quit = 1;
                break;
            }

            letterEntered = guess[0];
            reguessed = 0;

            printf("letterEntered:%c\n", letterEntered);

            oldCorrect = numCorrect;

            for (loopIndex = 0; loopIndex < lengthOfWord; loopIndex++) {

                if (letterGuessed[loopIndex] == 1) {
                    if (WordList[randomIndex][loopIndex] == letterEntered) {
                        reguessed = 1;
                        break;
                    }
                    continue;
                }

                if (letterEntered == WordList[randomIndex][loopIndex]) {
                    letterGuessed[loopIndex] = 1;
                    numCorrect++;
                }

            }

            if (oldCorrect == numCorrect && reguessed == 0) {
                numLives--;
                printf("Sorry, wrong guess\n");
                if (numLives == 0) {
                    break;
                }
            } else if (reguessed == 1) {
                printf("Already Guessed!!\n");
            } else {
                printf("Correct guess :)\n");
            }

        } // while loop

        if (quit == 1) {
            printf("\nthe user quit early\n");
        } else if (numLives == 0) {
            printf("\nSorry you lose, the word was: %s\n",
                    WordList[randomIndex]);
        } else {
            printf("\nYOU WIN!!! :)\n");
        }

        return 0;
    }

WordsReadIn doesn't seem to be incremented, possibly resulting in a divide by 0.

This loop:

    while (fgets(input, 100, pToFile)) {
        sscanf(input, "%s", WordList[WordsReadIn]);

    }

I'd write as:

    while (fgets(input, 100, pToFile)) {
        sscanf(input, "%59s", WordList[WordsReadIn]);
        ++WordsReadIn;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM