Greetings everyone, the code runs fine for the first game with the first word, h
ID: 3815115 • Letter: G
Question
Greetings everyone, the code runs fine for the first game with the first word, however, im having issues when i tried to go the second word in the hangwords.txt file (the words are: kind, double, print, extra, word). here is my code, i hope you could help me fix the error.
#define _CRT_SECURE_NO_DEPRECATE
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include <stdlib.h>
#define MAXGUESSES 6
#define SIZE 20
//Declares the function HowToPlay()
void HowToPlay();
//Declares the displayArrayr()
void DisplayArrayr(char wrd[]);
//Declares the DisplayWIP()
void DisplayWIP(char wrd[], int numLetters);
//Declares the function to play again
char playAgain();
//Declares the PlaceGuess()
int PlaceGuess(char arraya[], char arrayb[]);
//Declares the MatchIt()Function
int MatchIt(char wrd[], char guess, int isamatch);
//Declares the function WordToGuessedWord()
void WordToGuessedWord(char wrd[], char guess, int isamatch, int result);
//Declares the WinOrLose()
void WinOrLose(int Same, int numguess);
//Declares the Exit()
void Exit(char *quit);
int main()
{
//Declare the vriables
char fileptr[SIZE] = { '' };
char grd[SIZE]= { '' };
char wrd[SIZE]={ '' };
char guess = ' ';
int numguess = 0;
int isamatch = 0;
int Same = 0;
int result = 0;
char Quits;
FILE *fp;
//calls the HowToPlay()
HowToPlay();
printf(" -------------------- ");
//file name gest
fp = fopen("hangman.txt", "r");
fscanf(fp, "%s", fileptr);
//Copying the length
isamatch = strlen(fileptr);
DisplayWIP(wrd, isamatch);
//Dowhile
do
{
if (numguess<MAXGUESSES)
{
guess = tolower(playAgain());
grd[numguess] = (guess);
printf(" Letters guessed so far: ");
DisplayArrayr(grd);
result = MatchIt(fileptr, guess, isamatch);
printf("wresult=%d", result);
WordToGuessedWord(wrd, guess, isamatch, result);
Same = PlaceGuess(fileptr, wrd);
WinOrLose(Same, numguess);
}
else if (numguess>MAXGUESSES && Same == 1)
{
printf("You have run out of guesses ");
WinOrLose(Same, numguess);
Exit(&Quits);
}
//checks the number of guesses
numguess++;
} while (Quits != 'N'&&Quits != 'n');
fclose(fp);
return 0;
}
//Instructions for displaying the user
void HowToPlay()
{
printf("WELCOME TO HANGMAN! ");
printf("Please read the following instructions before you play. ");
printf("-You will be presented with a word to be guessed ");
printf("-Guess letters one at a time");
printf("-You can have up to six incorrect guesses ");
printf("-The game will be OVER when you have guessed ");
printf(" all the letters in the word or when you have guessed incorrectly SIX times ");
printf(" HAVE FUN! ");
}
//function to displays the character
void DisplayArrayr(char wrd[])
{
printf("%s ", wrd);
}
//Function to display the aterists
void DisplayWIP(char wrd[], int isamatch)
{
printf("GUESS THIS WORD: ");
int i;
for (i = 0; i < isamatch; i++)
{
wrd[i] = '*';
printf("%c", wrd[i]);
}
wrd[i] = '';
}
//Performs the user to play again
char playAgain()
{
char guess;
printf(" Please enter a letter:");
scanf(" %c", &guess);
return guess;
}
//Function to placeGuess()
int PlaceGuess(char arraya[], char arrayb[])
{
int Same;
Same = strcmp(arraya, arrayb);
return Same;
}
//Function to MatchIt()
int MatchIt(char wrd[], char guess, int isamatch)
{
int i;
for (i = 0; i < isamatch; i++)
{
if (wrd[i] == guess)
{
printf("That letter is in the word ");
return i;
}
}
printf("That letter is NOT in the word ");
return -1;
}
//Function to WinOrLose()
void WinOrLose(int Same, int numguess)
{
if (numguess == 0 && Same == 1)
{
printf("I'm sorry");
}
else if (Same == 0)
{
printf("Great Job!! You Win! ");
}
}
//Function to guess the word
void WordToGuessedWord(char wrd[], char guess, int isamatch, int result)
{
if (result != -1)
wrd[result] = guess;
printf(" %s ", wrd);
}
//Function to exit the program
void Exit(char *Quits)
{
printf("Would you like to play again? ");
printf("Type Q to QUIT or anything else to continue ");
scanf(" %c", Quits);
}
Explanation / Answer
It won't work because you only have a single while loop that asks for user's guess until the word is complete. What will happen next? It just closes the file and terminates! What happening here is, you are just reading one word and playing one game, then exiting!
To make it work you need to enclose the current do-while loop in another outer loop which will fetch a new word from the file and play the game again.
char fileptr[SIZE];
FILE *fp;
while (!feof(fp) && fscanf(fp, "%s", fileptr) == 1) {
........ // Declare variables here
char grd[SIZE];
char wrd[SIZE];
char guess = ' ';
int numguess = 0;
int isamatch = 0;
int Same = 0;
int result = 0;
char Quits;
do {
......
if (numguess<MAXGUESSES)
{
.....
}
else if (numguess>MAXGUESSES && Same == 1) {
.... // break here
}
} while (true);
........
// Check for termination here
Exit(&Quits);
if (Quits != 'N'&&Quits != 'n') {
break;
}
........
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.