Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Im doing a hangman project and I get a debug assertion failed and I believe it i

ID: 3908379 • Letter: I

Question

Im doing a hangman project and I get a debug assertion failed and I believe it is my while loop? PLZ HELP

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

#include <stdbool.h>

#include <ctype.h>

int main(void) {

const int MAX_MISSES = 7;

char guess[2];

char lettersGuessed[27];

char puzzlePhrase[50];

char displayPhrase[50];

int numOfMisses;

int numOfUnsolvedCharcters;

int i;

bool guessFound;

printf("Hangman Time!! ");

printf("-------------- ");

printf("Player 1 enter puzzle phrase(up to 50 characters): ");

gets_s(puzzlePhrase, 50);

for (i = 0; i <= 25; ++i) {

printf(" ");

}

strcpy_s(displayPhrase, 27, puzzlePhrase);

for (i = 0;i < strlen(displayPhrase); ++i) {

if (displayPhrase[i] != ' ') {

displayPhrase[i] = '_';

}

}

numOfUnsolvedCharcters = strlen(puzzlePhrase);

numOfMisses = 0;

strcpy_s(lettersGuessed, 27, "");

while ((numOfUnsolvedCharcters > 0) && (numOfMisses < MAX_MISSES)) {

if (numOfMisses == 0) {

printf("----------");

printf(" ");

printf("| | ");

for (i = 0;i < 7; i++) {

printf("| ");

}

printf("|");

printf("___________ ");

}

else if (numOfMisses == 1) {

printf("----------");

printf(" ");

printf("| | ");

printf(" O");

for (i = 0;i < 7; i++) {

printf("| ");

}

printf("|");

printf("___________ ");

}

else if (numOfMisses == 2) {

printf("----------");

printf(" ");

printf("| | ");

printf(" O");

printf(" /");

for (i = 0;i < 7; i++) {

printf("| ");

}

printf("|");

printf("___________ ");

}

else if (numOfMisses == 3) {

printf("----------");

printf(" ");

printf("| | ");

printf(" O");

printf(" /");

printf(" ''");

for (i = 0;i < 7; i++) {

printf("| ");

}

printf("|");

printf("___________ ");

}

else if (numOfMisses == 4) {

printf("----------");

printf(" ");

printf("| | ");

printf(" O");

printf(" /");

printf(" ''");

printf(" |");

for (i = 0;i < 7; i++) {

printf("| ");

}

printf("|");

printf("___________ ");

}

else if (numOfMisses == 5) {

printf("----------");

printf(" ");

printf("| | ");

printf(" O");

printf(" /");

printf(" ''");

printf(" |");

printf(" |");

for (i = 0;i < 7; i++) {

printf("| ");

}

printf("|");

printf("___________ ");

}

else if (numOfMisses == 6) {

printf("----------");

printf(" ");

printf("| | ");

printf(" O");

printf(" /");

printf(" ''");

printf(" |");

printf(" |");

printf(" /");

for (i = 0;i < 7; i++) {

printf("| ");

}

printf("|");

printf("___________ ");

}

else if (numOfMisses == 7) {

printf("----------");

printf(" ");

printf("| | ");

printf(" O");

printf(" /");

printf(" ''");

printf(" |");

printf(" /");

printf(" ''");

for (i = 0;i < 7; i++) {

printf("| ");

}

printf("|");

printf("___________ ");

}

printf("Guesses: %s ", lettersGuessed);//May need to change this to add %c

printf("Phrase: %s ", displayPhrase); //May need to change this to add %s

printf("Guess a letter:");

gets_s(guess, 2);

while (!isalpha(guess[i])) {

gets_s(guess, 2);

}

strcat_s(lettersGuessed, 27, guess);

printf("Guesses: %s ", lettersGuessed);//May need to change this to add %c

printf("Phrase: %s ", displayPhrase); //May need to change this to add %s

printf("Guess a letter:");

guessFound = false;

for (i = 0; i < strlen(puzzlePhrase); ++i) {

if (toupper(puzzlePhrase[i]) == toupper(guess[0])) {

displayPhrase[i] = guess[0];

--numOfUnsolvedCharcters;

guessFound = true;

}

if (!guessFound) {

++numOfMisses;

}

}

}

printf("%s", displayPhrase);

if (numOfUnsolvedCharcters > 0) {

printf("NOT GOOD AT GUESSING I SEE - BETTER LUCK NEXT TIME :) ");

}

else {

printf("WINNER!!! ----- PLAY AGAIN ;) ");

}

return 0;

}

Explanation / Answer

#include /*Header file declaration*/ #include /* for strcmp();,strlen(); functions use*/ #include void showHangman(int); int main(void) { char hangmanWord[100], tempWord[100]; /**hangmanWord[] array for the original word and tempWord[] array to get the alphabet from user and compare it with original word**/ char hangmanOutput[100]; /**This array will show the remaining blanks and correct inputs**/ int wrongTry = 6 , matchFound = 0; /**player will get 5 chance, so we use wrongTry as chance counter**/ /**matchFound to search the alphabet, if the alphabet from user does not exist in the original word it will remain 0, upon finding the word, matchFound will be set as 1**/ int counter = 0 , position = 0, winner, length , i; char alphabetFromUser; system("cls"); /**for clearing the screen**/ printf(" Enter any word in small case and hit >>ENTER>ENTERENTER "); fflush(stdin); scanf("%c",&alphabetFromUser); /**get alphabet from user**/ if(alphabetFromUser < 'a' || alphabetFromUser > 'z') /**In case player gives input other than 'a' to 'z' the console will ask again**/ { system("cls"); printf(" Wrong input TRY AGAIN "); matchFound = 2; } fflush(stdin); if (matchFound != 2) { for(counter=0;counter