Here is the code provided by chegg but not working. Please, provide a good code
ID: 3769967 • Letter: H
Question
Here is the code provided by chegg but not working. Please, provide a good code or fix that one, please
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <time.h>
#define NUM_OF_WORDS 25
#define NUM_OF_CHANCES 5
#define TRUE 1
#define FALSE 0
void show_heading_rules();
void scold_and_quit();
void see_in_word(char, const char [], char *, const size_t, unsigned int *);
void show_word_clue(const size_t);
void show_heading_rules()
{
printf(" HANGMAN ");
printf("Hangman is a game where you are expected to guess a word in a certain number of chances that is"" before you hang the hangman. ");
printf("A word has been chosen, begin guessing it. Good luck ");
printf("Guess the Word: ");
}
void scold_and_quit()
{
printf(" You killed a person, you dumb shit! Good Bye. ");
exit(0);
}
void show_word_clue(const size_t len)
{
size_t i;
for(i = 0; i < len; i++)
printf(" _ ");
printf(" ");
}
void see_in_word(char guessed, const char word[], char *a, const size_t len, unsigned int *found)
{
size_t i;
for(i = 0; i < len; i++)
{
if(guessed == word[i])
{
*(a + i) = guessed;
*found = TRUE;
}
else
{
if(*(a + i) >= 65 && *(a + i) <= 90 ) ;
else if(*(a + i) >= 97 && *(a + i) <= 122) ;
else *(a + i) = '_';
}
}
for(i = 0; i < len; i++)
{
printf("%c ", *(a + i));
}
if(strcmp(a, word) == 0)
{
printf(" You did it. Well done. ");
exit(0);
}
return;
}
int main()
{
char *hanged[]={
"|=====| "
" | "
" | "
" | "
" === ",
"|=====| "
"O | "
" | "
" | "
" === ",
"|=====| "
"O | "
"| | "
" | "
" === ",
"|=====| "
"O | "
"| | "
"| | "
" === ",
" |=====| "
" O | "
" | | "
"/ \ | "
" === "
};
char *words[] = {
"APPLE", "BALL", "CAT", "DOG","ELEPHANT","FAN","GATE","HAT", "INK", "JACKLE", "KITE", "LION",
"MAN", "NOSE", "PARROT", "QUEEN", "RAT", "STATE", "TAXI", "UMBRELLA", "VAN", "WATCH", "XYLOPHONE",
"YAK", "ZEBRA"
};
show_heading_rules();
srand((unsigned) time(NULL));
unsigned short chosen_word = rand() % NUM_OF_WORDS;
char guessed;
size_t len = strlen(words[chosen_word]);
size_t i = 0;
unsigned int found = FALSE;
unsigned int chances = 0;
show_word_clue(len);
char *a = (char *)malloc(len + 1);
printf(" Guess a letter: ");
while(1)
{
guessed = toupper(getchar());
if(guessed != ' ')
{
printf(" Guess a letter: ");
see_in_word(guessed, words[chosen_word], a, len, &found);
if(found == FALSE)
{
if(chances >= 4)
{
printf(" %s ", hanged[chances++]);
scold_and_quit();
}
printf(" Too bad . That was wrong ");
printf(" %s ", hanged[chances++]);
}
found = FALSE;
}
}
}
Explanation / Answer
corrected the code with compiler check out
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <time.h>
#define NUM_OF_WORDS 25
#define NUM_OF_CHANCES 5
#define TRUE 1
#define FALSE 0
void show_heading_rules();
void scold_and_quit();
void see_in_word(char, const char [], char *, const size_t, unsigned int *);
void show_word_clue(const size_t);
void show_heading_rules()
main()
{
clrscr();
{
printf(" HANGMAN ");
printf("Hangman is a game where you are expected to guess a word in a certain number of chances that is"" before you hang the hangman. ");
printf("A word has been chosen, begin guessing it. Good luck ");
printf("Guess the Word: ");
}
void scold_and_quit()
{
printf(" You killed a person, you dumb shit! Good Bye. ");
exit(0);
}
void show_word_clue(const size_t len)
{
size_t i;
for(i = 0; i < len; i++)
printf(" _ ");
printf(" ");
}
void see_in_word(char guessed, const char word[], char *a, const size_t len, unsigned int *found)
{
size_t i;
for(i = 0; i < len; i++)
{
if(guessed == word[i])
{
*(a + i) = guessed;
*found = TRUE;
}
else
{
if(*(a + i) >= 65 && *(a + i) <= 90 ) ;
else if(*(a + i) >= 97 && *(a + i) <= 122) ;
else *(a + i) = '_';
}
}
for(i = 0; i < len; i++)
{
printf("%c ", *(a + i));
}
if(strcmp(a, word) == 0)
{
printf(" You did it. Well done. ");
exit(0);
}
return;
}
int main()
{
char guessed;
char *hanged[]={
unsigned char chosen_word = rand() % NUM_OF_WORDS;
"|=====| "
" | "
" | "
" | "
" === "
"|=====| "
"O | "
" | "
" | "
" === "
"|=====| "
"O | "
"| | "
" | "
" === "
"|=====| "
"O | "
"| | "
"| | "
" === "
" |=====| "
" O | "
" | | "
"/ \ | "
" === "
}
extern char *words[] = {
"APPLE", "BALL", "CAT", "DOG","ELEPHANT","FAN","GATE","HAT", "INK", "JACKLE", "KITE", "LION",
"MAN", "NOSE", "PARROT", "QUEEN", "RAT", "STATE", "TAXI", "UMBRELLA", "VAN", "WATCH", "XYLOPHONE",
"YAK", "ZEBRA"
};
show_heading_rules();
srand((unsigned) time(NULL));
//unsigned short chosen_word = rand() % NUM_OF_WORDS;
//extern char size_t len = strlen(words[chosen_word]);
size_t i = 0;
unsigned int found = FALSE;
unsigned int chances = 0;
show_word_clue(len);
char *a = (char *)malloc(len + 1);
printf(" Guess a letter: ");
while(1)
{
guessed = toupper(getchar());
if(guessed != ' ')
{
printf(" Guess a letter: ");
see_in_word(guessed, words[chosen_word], a, len, &found);
if(found == FALSE)
{
if(chances >= 4)
{
printf(" %s ", hanged[chances++]);
scold_and_quit();
}
printf(" Too bad . That was wrong ");
printf(" %s ", hanged[chances++]);
}
found = FALSE;
}
}
}
getch();
}