当试图在日食上运行程序时它会崩溃

时间:2013-12-11 20:56:35

标签: c eclipse

我认为在尝试读取.txt文件时可能会出现某种内存泄漏。它只是停止响应和崩溃我对编程一般都是新手。

这是完整的不完整代码:

#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;
    }

1 个答案:

答案 0 :(得分:1)

WordsReadIn似乎没有增加,可能导致除以0。

这个循环:

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

    }

我写的是:

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