Ok, changed the play_again() function but it's still not advancing once I've actually guess the letters in the word, it continues to ask me to guess a letter, even when the word is already guessed. Here's the code again, please, any help would be MUCH appriciated
Code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX_SIZE = 20;
#define MAX_LETTERS = 26;
#define MAX_GUESS = 6;
void instruct();
int play_again();
void initialize();
int letter_check(char c);
int won();
char word_to_guess[20], word_in_prog[20], letters_guessed[26];
int guess;
int main()
{
int i;
char c;
FILE *in_file;
in_file = fopen("wordlist.txt","r");
do {
if( feof(in_file) )
rewind(in_file);
fscanf(in_file, "%s", word_to_guess);
for(i = 0; i < strlen(word_to_guess); i++) {
word_to_guess[i] = tolower(word_to_guess[i]);
}
initialize();
instruct();
do {
printf("\n Number of tries left : %d", guess);
printf("\n %s", letters_guessed);
printf("\n Word to guess : ");
for(i = 0; i<strlen(word_in_prog); i++)
printf("%c ", word_in_prog[i]);
printf("\n Please guess a letter : ");
scanf("%c", &c);
c = tolower(c);
if( !letter_check(c) )
guess--;
} while( guess > 0 || !won() );
if( guess == 0 )
printf("\n You LOSE!");
} while(play_again());
fclose(in_file);
}
void instruct()
{
printf("\n Hangman v6.9");
printf("\n To play, guess a letter. You get 6 wrong tries, then your man gets hung. Is that a bad thing?");
}
int play_again(void) // If your function accepts 0 arguments, let the compiler know.
{
int c; // getchar() returns an int
int garbage; // For flushing the input buffer
printf("\n Would you like to play again? [y/N]"); // Common notation for showing a default choice
fflush(stdout); // Best to flush stdout after a prompt
c = getchar(); // Get the user's choice
while ((garbage = getchar()) != '\n' && garbage != EOF); // Flush the input buffer
c = tolower(c); // Proper use of tolower(). It returns the new value; it doesn't modify it.
return c == 'y'; // No need for the whole if/else construct. Will evaluate to 1 if true, 0 if false
}
void initialize()
{
int i;
guess = 6;
for(i=0; i<26; i++) {
letters_guessed[i] = '-';
}
for(i=0; i<strlen(word_to_guess); i++) {
word_in_prog[i] = '_';
}
}
int letter_check(char c)
{
int i, flag = 0;
for(i = 0; i<strlen(word_to_guess); i++) {
if(c == word_to_guess[i]) {
flag = 1;
word_in_prog[i] = c;
}
}
return flag;
}
int won()
{
int flag = 0;
if(strcmp(word_to_guess, word_in_prog)) {
printf("\n You've WON! %s is the right answer!", word_in_prog);
flag = 1;
}
return flag;
}