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
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
#include
#include
#include
#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] = { '\0' };
char grd[SIZE]= { '\0' };
char wrd[SIZE]={ '\0' };
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 { 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] = '\0'; } //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); }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
