[英]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. 我认为尝试读取.txt文件时可能是某种形式的内存泄漏。 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() { } 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. WordsReadIn
似乎没有增加,可能会导致被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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.