Trouble using Functions for Hangman game in C -


i trying make hangman gave using c functions. able make game without using functions when try use functions running few problems.

the main problem having in displayword() function. need function print out word screen letters have not been correctly guessed blanked out underscore.

~header fiiles

#include <stdio.h> #include <stdlib.h> #include <string.h>  /* constants. */ #define num_words 50 #define alphabet_size 26 #define good_guess 0 #define bad_guess 1 #define game_over 1 #define game_continue 0 #define max_guess 10 #define max_word_len 10  /* function prototypes. */ int init(char* word); void displayword(char* word, int* guessedletters, int* length); int guessletter(char* word, int* guessedletters); void displayhangman(unsigned wrongguesses); int isgameover(char* word, int* guessedletters, unsigned wrongguesses); void readrestofline(); 

~main code

#include "hangman.h"  /**************************************************************************** * function main() entry point program. ****************************************************************************/ int main(void) {    char word[max_word_len + 1];    unsigned wrongguesses = 0;    int guessedletters[alphabet_size] = {       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0    };    int length = 0;     int numlives = 10;    int numcorrect = 0;    int oldcorrect = 0;    int lengthofword = 0;    length = init(word);    printf("length: %d\n", length);    displayword(word, guessedletters, length);       return exit_success; }   int init(char* word) {    srand(time(null));    int randomindex = rand() % 50;     const char* words[num_words] = {       "array",      "auto",       "break",      "case",       "cast",       "character",  "comment",    "compiler",   "constant",   "continue",       "default",    "double",     "dynamic",    "else",       "enum",       "expression", "extern",     "file",       "float",      "function",       "goto",       "heap",       "identifier", "library",    "linker",       "long",       "macro",      "operand",    "operator",   "pointer",       "prototype",  "recursion",  "register",   "return",     "short",       "signed",     "sizeof",     "stack",      "statement",  "static",       "string",     "struct",     "switch",     "typedef",    "union",       "unsigned",   "variable",   "void",       "volatile",   "while"    };      word = words[randomindex];     int lengthofword = strlen(words[randomindex]);     printf("word: %s\n", words[randomindex]);      return lengthofword;    }  void displayword(char* word, int* guessedletters, int* length) {      int loopindex;     char gameword[][1] = word;       ( loopindex = 0; loopindex < length; loopindex++ ) {          if(letterguessed[loopindex] == 1) {             printf("%c",gameword[0][loopindex]);         } else {             printf("-");         }      } } 

i have no idea how scan through random word print out letters have been guessed. have tried few different way reading around web , watching videos not sure. please help.

problem code

void displayword(char* word, int* guessedletters, int* length) {      int loopindex;     char gameword[][1] = word;       ( loopindex = 0; loopindex < length; loopindex++ ) {          if(letterguessed[loopindex] == 1) {             printf("%c",gameword[0][loopindex]);         } else {             printf("-");         }      } } 

first thing notice: in void displayword(char* word, int* guessedletters, int* length) you're accepting int pointer named lenght, in main code passing normal int.

second thing, in displayword() have loop length of length of word string. when length 8, loop goes 8 times , not check whole guessedletters[] array.

third thing in init(char *word) function, passing pointer. in scope of function, variable word a copy of original pointer when point other locations, not have effect. want change

 word = words[randomindex]; 

to

 strcpy(word, words[randomindex]); 

now give example of solution can change code so:

void displayword(char *word, int *guessedletters, int length) {     int i;      (i = 0; < length; i++ ){          if(guessedletters[word[i] - 97] == 1){             printf("%c", word[i]);         }else{             printf("-");         }        } } 

see demo

what did here in guessedletters array check if current character guessed or not. subtracted 97 current character because decimal ascii value of 'a' (and when current character 'a', @ index 0).


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -