Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Why doesn't the code run everything?

+1 vote
asked Jun 15 by Tamara Benjamin (130 points)
The code is for a hangman game but only displays hangman and stops.

#include <stdio.h>

#include <stdlib.h>

#include <ctype.h>

#include <math.h>

#include <time.h>

#include <string.h>

char guessWord();

int isAlreadyGuessed(char);

int setBlanks(char);

int checkIfWon();

void printBoard();

void pickword();

void reset();

int playAgain();

int isInWord(char, char*);

char* secret = NULL;

char* blanks = NULL;

char* guessingBank = NULL;

int position;

int length;

char* board[] = {"  O", "  O\n  |", "  O\n /|", "  O\n /|\\", "  O\n /|\\\n /", "  O\n /|\\\n / \\",};

int main(int argc, char** argv) {

puts("HANGMAN:\n");

do {

reset();

while(position < 6) {

printBoard();

char guess = guessWord();

if(isAlreadyGuessed(guess)) continue;

else if(setBlanks(guess)) {

if(checkIfWon()) break;

} else {

guessingBank[position++]=guess;

}

}

if(position >= 6) printf("You lost the word was %s\n", secret);

} while(playAgain());

free(secret);

free(blanks);

free(guessingBank);

return 0;

}

void reset() {

position = 0;

pickword();

}

void printBoard() {

puts(board[position]);

printf("\n%s:\n", blanks);

}

char guessWord() {

puts("Enter a letter\n");

char c = getchar();

if(getchar() != '\n') {

puts("Please enter only 1 letter next time\n");

while(getchar() != '\n');

} else if(isalpha(c)) return tolower(c);

return guessWord();

}

int isAlreadyGuessed(char guess) {

if(isInWord(guess, guessingBank)) {

puts("You already guessed that letter\n");

return 1;

} return 0;

}

int setBlanks(char guess) {

int boolean = 0;

for(int i = 0; i < length; ++i) {

if(secret[i] == guess) {

blanks[i] = guess;

boolean = 1;

}

}

return boolean;

}

int playAgain() {

puts("Enter y to play again\n");

char c = getchar();

while(getchar() != '\n');

return tolower(c) == 'y';

}

int checkIfWon() {

if(strcmp(secret, blanks) == 0) {

printf("You got it man! it was %s\n", secret);

return 1;

} return 0;

}

int isInWord(char guess, char* word) {

for(int i = 0; i < position; ++i) {

if(word[i] == guess) return 1;

} return 0;

}

void pickword() {

FILE* file;

int lines = 0;

char c = 0;

file = fopen("words.txt", "r");

while((c=getc(file)) != EOF) if(c == '\n') ++lines;

fseek(file, 0, SEEK_SET);

srand(time(NULL));

int randomLine = 1 + rand() % lines;

int seekWhere = 0;

int characters = 0;

lines = 0;

c = 0;

while(lines < randomLine && (c=getc(file)) != EOF) {

if(lines+1 == randomLine) {

++characters;

} else {

++seekWhere;

}

if(c == '\n') ++lines;

}

--characters;

length = characters;

if(secret == NULL && blanks == NULL && guessingBank == NULL) {

secret = (char*) malloc(characters+1);

blanks = (char*) malloc(characters+1);

guessingBank = (char*) malloc(7);

} else {

secret = realloc(secret, characters+1);

blanks = realloc(blanks, characters+1);

}

secret[characters] = '\0';

blanks[characters] = '\0';

guessingBank[6] = '\0';

fseek(file, seekWhere, SEEK_SET);

fread(secret, 1, characters, file);

for(int i = 0; i < characters; ++i) {

blanks[i] = '-';

}

for(int i = 0; i < 6; ++i) {

guessingBank[i] = ' ';

}

fclose(file);

}

1 Answer

0 votes
answered Jul 3 by xDELLx (8,580 points)
file = fopen("words.txt", "r");

while((c=getc(file)) != EOF) if(c == '\n') ++lines;


words.txt, if this file is missing in your current directory [home directory] the program crashes & will just print a single line u see..

correct it by checking we are able to successfully find & open the words.txt file.

something like below


    file = fopen("words.txt", "r");
   if (!file) {printf("Where is the words.txt?? \nCouldnt find it !!\nWill play only if you find it for me :) \n;"); exit(1);}
  while((c=getc(file)) != EOF) if(c == '\n') ++lines;

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...